[英]jQuery from google CDN is not working
I want to use jQuery in my webpage. 我想在我的网页中使用jQuery。 I don't want to download it, so I am including it from Google's CDN. 我不想下载它,所以我从Google的CDN中包含了它。
When I run the page, the jQuery is not working. 当我运行页面时,jQuery无法正常工作。
Can anyone tell me whether the problem relies in jQuery or in my code. 谁能告诉我问题出在jQuery还是我的代码中。
Here is my code. 这是我的代码。
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
cars=new Array("Mercedes","Volvo","BMW","porche");
phones=new Array('Samsung','Nokia','Iphone');
populateSelect();
$(function() {
$('#cat').change(function(){
populateSelect();
});
});
function populateSelect(){
cat=$('#cat').val();
$('#item').html('');
if(cat=='car'){
cars.forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}
if(cat=='phone'){
phones.forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}
}
</script>
</head>
<body>
<select id="cat">
<option value="car">car</option>
<option value="phone">phone</option>
</select>
<select id="item">
</select>
</body>
</html>
You call populateSelect() which tries to access your select tag when the DOM hasn't even loaded yet. 您调用populateSelect(),当DOM尚未加载时,它尝试访问您的选择标签。
Move the function call into your DOM Ready event handler: 将函数调用移到DOM Ready事件处理程序中:
$(function() {
populateSelect();
$('#cat').change(function(){
populateSelect();
});
});
The next time, open your browser's developer console ! 下次,打开浏览器的开发人员控制台 ! It will clearly show the error. 它将清楚地显示错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.