为什么这个 mouseover jquery 代码不起作用?
我在网站的头部区域使用以下代码(我也尝试过正文):
<script>
$(document).ready(function() {
$(function(){
$("#h1").mouseover(function () {
$("#h1").css("color","red");
});
});
});
</script>
我也使用它作为 div(按钮):
<div class="button" id="h1"><strong>Home</strong></div>
为什么当我将鼠标悬停在上面时字体没有变为红色它? (原色为白色仅供参考)
I'm using the following code in the head area of the site (I've also tried the body):
<script>
$(document).ready(function() {
$(function(){
$("#h1").mouseover(function () {
$("#h1").css("color","red");
});
});
});
</script>
I'm also using this as the div (button):
<div class="button" id="h1"><strong>Home</strong></div>
Why isn't the font changing to red when I mouse over it? (Original color is white fyi)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在内联脚本代码运行时,jQuery 尚未找到任何可将鼠标悬停绑定到的结果。
您需要将内联脚本包装在 document.ready 调用中,如下所示:
准备好 jQuery 的就绪函数
At the time the inline script code runs, jQuery hasn't found any results to bind the mouseover to.
You need to wrap your inline script in a document.ready call like this:
Ready about jQuery's ready function
您需要将其包装在
document.ready
内。问题是您试图在创建元素之前将处理程序附加到该元素。您应该阅读文档以准备好更好地理解:
http://api.jquery.com/ready/< /a>
You need to wrap that inside of
document.ready
. The issue is that you are attempting to attach a handler to an element prior to it being created.You should read the doc for ready to get a better understanding:
http://api.jquery.com/ready/