JQuery - 如何在 DIV 列表上使用悬停()事件?
我有以下 HTML:
<div id="panel">
<div class="listing" id="ref_1">...</div>
<div class="listing" id="ref_2">...</div>
<div class="listing" id="ref_3">...</div>
<div class="listing" id="ref_4">...</div>
</div>
我想做的是,当有人将鼠标悬停在 div.listing
上时,将 alert()
发送到屏幕 id< /代码> 名称。
意思是,如果有人将鼠标悬停在 id="ref_3"
上,就会 alert("ref_3")
;
问题:如何使用 JQuery/Javascript 执行此操作?
更新:
下面的链接是实时网站。正如您将看到的,下面的答案都不起作用。 (第340行)
有什么推荐吗?
I have the following HTML:
<div id="panel">
<div class="listing" id="ref_1">...</div>
<div class="listing" id="ref_2">...</div>
<div class="listing" id="ref_3">...</div>
<div class="listing" id="ref_4">...</div>
</div>
What I should like to do is, when someone hovers over a div.listing
, to alert()
to the screen the id
name.
Meaning, if a person hovers over with their mouse the id="ref_3"
, to alert("ref_3")
;
Question: How do I do this with JQuery/Javascript?
UPDATE:
Linked below is the live site. As you'll see, none of the answers below work. (Line 340)
Any recommendation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以在此处查看此代码的运行情况。
更新
查看您的代码,您可能想尝试以下操作:
You can see this code working here.
UPDATE
looking at your code, you might want to try this instead:
您当前是否在其他脚本文件中使用 $ 作为函数,而不是使用 noConflict
Are you currently using $ as a function in other script file, and not using noConflict
BillyJ,听起来您可能没有在 HTML 文件中加载 jQuery 库。
请务必包含
BillyJ, it sounds like you maybe aren't loading up the jQuery library in your HTML file.
Be sure to include
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"></script>
in your file before calling the above function.更好的是,
这有效
better yet
this works
似乎与您的网站分开工作正常...
我建议只在代码中添加一个带有
hslisting
类的 div 。不要使用JS来注入它。看看您的注射方式是否导致了问题。http://jsbin.com/agosa 效果很好。
Seems to work fine separate from your site...
I'd suggest just adding a div in the code with a
hslisting
class. Don't use JS to inject it. See if something about the way you are injecting it is causing problems.http://jsbin.com/agosa works just fine.
“mouseenter”事件通常是比“mouseover”更好的选择。从
http://api.jquery.com/mouseenter/ :
The 'mouseenter' event is usually a better choice than 'mouseover'. From
http://api.jquery.com/mouseenter/ :