嵌套 div 中的元素,jquery 事件不起作用
我想在某些嵌套 div 中使用锚元素的事件,但我的代码中有些东西不起作用。
我在该选择器上尝试了数百种变体,但它仍然不起作用。
html代码:
<div class="tabContents">
<div class="thumbArea">
<a href="#">
<img src="foo" alt="baba"/>
</a>
</div>
<div class="imageArea">
</div>
</div>
JS:
$(function(){
$(".tabContents a").hover(function() {
alert("just work!");
});
});
i want to use an event of an anchor element in some nested div's but something is not working in my code.
i tried hundreds of variations on that selector but it still does not work.
html code:
<div class="tabContents">
<div class="thumbArea">
<a href="#">
<img src="foo" alt="baba"/>
</a>
</div>
<div class="imageArea">
</div>
</div>
JS:
$(function(){
$(".tabContents a").hover(function() {
alert("just work!");
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jQuery
hover
有两个函数,一个“over”和一个“out”。如果您只是寻找鼠标悬停,我建议:
进一步响应:
尝试使用 jQuery 的
live
事件。这将确保事件侦听器也会关注添加到 DOM 的任何新元素(例如您要附加的元素)。不过,live 目前不支持悬停
。您可以执行mouseover
和mouseout
事件来达到相同的效果。我想我看到有人为 jQuery 编写了一个扩展,允许在
live
中使用“hover”来回答这里的问题,所以这是可以做到的,但是可惜,我不能似乎找到了。jQuery
hover
takes two functions, an "over" and an "out".If you're just looking for mouseover, I would suggest:
Further Response:
Try using jQuery's
live
event. This will ensure that the event listener will also be paying attention for any new elements added to the DOM (like the ones you're appending). However, live does not currently supporthover
. You can do amouseover
and amouseout
event though to achieve the same effect.I think I saw that somebody wrote an extension for jQuery that allowed for the use of 'hover' with
live
in a response to a question here on SO, so it can be done, but alas, I cannot seem to find it.你的代码——逐个字符地复制——对我来说非常有效。 (注意:Windows 上的 Firefox。)
尝试使用 firebug 并看看它是否可以帮助您确定发生了什么。
Your code -- copied character-for-character -- is working perfectly for me. (NOTE: Firefox on Windows.)
Try using firebug and see if it can help identify what's happening for you.