所以首先,我知道有几个答案,但是没有一个能解决我的问题。首先,我将展示我的代码HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Shop</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<script src="https://use.fontawesome.com/a565674a9d.js"></script>
<!--MINE-------------------------------->
<script src="JS/jquery.js"></script>
<script src="JS/catalog.js"></script>
</head>
<body>
<nav class="navbar navbar-light">
<div>
<a class="navbar-brand" style="text-align: center;position: fixed; left: 40%;" >
<img src="979709898.png" width="70" height="70" class="d-inline-block align-top" alt="">
Costumer Bay
</a>
<a id="cart" class=" navbar-brand btn btn-danger" style="position: fixed; left: 70%" >
<i class="fa fa-shopping-cart fa-3x" aria-hidden="true"></i>
</a>
</div>
</nav>
<div id="lin1" class="container-fluid" style="padding: 20px;">
</div>
<div id="lin2" class="container-fluid" style="padding: 20px;">
</div>
</body>
</html>jquery.js是未压缩的原始jquery源代码。
Data.Json在JS文件夹中
data.json:
[
{
"id":0,
"name":"Laptop",
"price":50000 ,
"quantity":0
},
{
"id":1,
"name":"Phone",
"price":30000 ,
"quantity":0
},
{
"id":2,
"name":"Desktop",
"price":70000 ,
"quantity":0
},
{
"id":3,
"name":"Headphone",
"price":9000 ,
"quantity":0
},
{
"id":4,
"name":"Tablet",
"price":35000 ,
"quantity":0
},
{
"id":5,
"name":"AntiVirus",
"price":5000 ,
"quantity":0
}
]最后,不起作用的javascript
catalog.js
let productinfo=[];
let firsttime = true;
$(function () {
console.log("here");
refreshcatalog();
console.log("now where");
let cart = $('#cart');
cart.click(function () { //NOT!WORKING
loadcart();
})
});
function refreshcatalog() {
console.log("no where");
if (firsttime){
console.log('evenhere');
$.getJSON("JS/data.json",function (data) {
console.log(data); //NOT!WORKING
});
}
if(firsttime){
firsttime=!firsttime;}
}
function loadcart() {
console.log("will load cart");
}甚至没有正在运行的单击事件。请指导我,不,我不知道AJAX,所以请不要使用ajax方法来解决。
请帮帮忙
<-编辑1
for all those asking for directory structure:
shopping-----
|_ catalog.html
|_ JS(folder) -----
|_ catalog.js
|_ jquery.js
|_ data.json<
在将格式更改为:
$(document).on('click','#button2', function()
{
alert("Dynamic button action");
});老旧的".click“不起作用的原因之一,将会非常感谢我使用火狐开发者版的.Btw。
发布于 2017-07-01 02:02:49
下面的代码需要更改,因为您的catalog.js & data.json驻留在同一个文件夹中。
$.getJSON("JS/data.json",function (data)
console.log(data);
});将代码更改为
$.getJSON("data.json",function (data) {
console.log(data);
});请检查并让我知道你是否能够安慰的数据,然后会解决另一个问题。
让我们重新排列脚本(排序)
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>我删除了jquery,因为引导程序使用的是它,而是将您的jquery复制到顶部,以便引导程序可以使用它。
,这是编辑的部分.
请不要从$.getJSON中删除JS/data.json。
删除jquery slim & JS/jquery。尝试使用下面的CDN链接或我在注释中提供的
在我的地方对我来说很管用。
https://stackoverflow.com/questions/44856099
复制相似问题