<分区>
标签 javascript jquery html css dom
<分区>
我是 JQuery 的新手,我正在尝试一些简单的代码,但没有在浏览器上获得所需的输出。这是我的代码:
<head>
<title>Product</title>
<script type="text/javascript" src="../../Scripts/jquery-1.5.1-vsdoc.js"></script>
</head>
<body>
<script type="text/javascript">
$('p').css('color','#red');
</script>
<p>
This is for test, supposed to show in red!!
</p>
</body>
我做错了什么?我用firefox来观察结果。提前致谢。
最佳答案
当 dom 尚未构建时,您尝试使用它。在 jQuery 中有一个标准的事件处理程序:
$(document).ready(function () {
$('p').css('color', 'red');
});
更新:另外正如此处提到的,您应该使用 red
而不是 #red
,因为 #
是用于颜色的十六进制代码。
关于javascript - 为什么不在浏览器上获得 JQuery 效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12344974/