Jquery 警报框不会弹出
我是jQuery新手,目前正在阅读jQuery for dummies,并尝试执行本书的第一个示例:Jquery 警报框不会弹出,jquery,Jquery,我是jQuery新手,目前正在阅读jQuery for dummies,并尝试执行本书的第一个示例: <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”> <html> <head> <title>My Test Page</title> <scr
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html>
<head>
<title>My Test Page</title>
<script type="text/javascript" src="js/jquery-1.7.2.js">
$(document).ready(function(){
alert(jQuery(‘img’).attr(‘alt’));
});
</script>
</head>
<body>
<p>This is my test page.</p>
<img src= "images/home.gif" height="28" width="28" alt="This is a test
image.">
</body>
</html>
我的测试页面
$(文档).ready(函数(){
警报(jQuery('img').attr('alt'));
});
这是我的测试页面。
但执行此示例后,不会弹出警报框。
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
alert($('img').attr('alt'));
});
</script>
$(文档).ready(函数(){
警报($('img').attr('alt'));
});
这应该可以解决问题。尝试更改
<script type="text/javascript" src="js/jquery-1.7.2.js">
$(document).ready(function(){
alert(jQuery(‘img’).attr(‘alt’));
});
</script>
$(文档).ready(函数(){
警报(jQuery('img').attr('alt'));
});
到
$(文档).ready(函数(){
警报(jQuery('img').attr('alt'));
});
您有多个问题
首先,您的示例显示“智能引号”(“/”)。这些都不是计算机世界中任何人都能理解的真实引用
其次,您试图错误地使用脚本标记
您需要将它们分成多个标记,一个用于嵌入,一个用于内联:
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
alert(jQuery('img').attr('alt'));
});
</script>
$(文档).ready(函数(){
警报(jQuery('img').attr('alt'));
});
您使用了奇怪的引号('
):
警报(jQuery('img').attr('alt'))
或使用双引号:
alert(jQuery("img").attr("alt"));
您不能在带有src
属性的脚本标记中编写javascript:
<script type="text/javascript" src="js/jquery-1.7.2.js"> </script>
<script type="text/javascript">
$(document).ready(function(){
alert(jQuery('img').attr('alt'));
});
</script>
$(文档).ready(函数(){
警报(jQuery('img').attr('alt'));
});
本地目录中可能未加载或缺少jquery-1.7.2.js
。请对jquery('img').attr('alt')使用双引号或单引号代码>而不是那些有趣的我没有注意到那些有趣的。:)+1正确的引语会让世界变得不同。它们并不奇怪,它们很聪明:)@VisioN。我猜它们太“聪明”了,javascript无法处理<代码>:)
@WouterH因为JavaScript对于智能引号来说太单调了:)
alert(jQuery("img").attr("alt"));
<script type="text/javascript" src="js/jquery-1.7.2.js"> </script>
<script type="text/javascript">
$(document).ready(function(){
alert(jQuery('img').attr('alt'));
});
</script>