问题
I have dynamically generated div tags which looks something like this:
<div class="widget widget_1">
<div class="widget_header widget_header_1"></div>
<div class="widget_sub_header widget_sub_header_1"></div>
<div class="widget_content widget_content_1"></div>
<div class="widget_footer widget_footer_1"></div>
<div>
<div class="widget widget_1">
<div class="widget_header widget_header_1"></div>
<div class="widget_sub_header widget_sub_header_1"></div>
<div class="widget_content widget_content_1"></div>
<div class="widget_footer widget_footer_1"></div>
<div>
<div class="widget widget_6">
<div class="widget_header widget_header_6"></div>
<div class="widget_sub_header widget_sub_header_6"></div>
<div class="widget_content widget_content_6"></div>
<div class="widget_footer widget_footer_6"></div>
<div>
These div tabs will be generated based on user selection, so I can't know how many they will have on screen at once, all I will know is that they can have many of the same widgets on the screen and/or difference once on the screen at once.
I am trying to get a hover effect when hovering over any of these widgets. I tried the follow, but nothing happens when hovering over .widget
.
$(".widget_content").on({
mouseenter: function () {
alert("enter");
},
mouseleave: function () {
alert("leave");
}
});
Any idea what I'm doing wrong?
回答1:
If the elements are dynamically added, use delegated events approach:
$("body").on({
mouseenter: function() {
alert("enter");
},
mouseleave: function() {
alert("leave");
}
}, ".widget_content");
Here instead of body
you may use any static parent element of .widget_content
.
回答2:
Working demo http://jsfiddle.net/Ex3M6/
Try in $(document).ready(function() {
Rest hope it fits your cause :)
Code
$(document).ready(function() {
$(".widget_content").on({
mouseenter: function() {
alert("enter");
},
mouseleave: function() {
alert("leave");
}
});
});
回答3:
Use this:
$(".widget_content").hover(
function () {
alert("enter");
},
function () {
alert("leave");
}
);
回答4:
Add text to your div tags otherwise on top of what are you mouse hovering, and your container div
tags should have close tags correctly as well(i.e. </div>
).
<div class="widget widget_1">
<div class="widget_header widget_header_1">dfsdfsdf</div>
<div class="widget_sub_header widget_sub_header_1">sdfsdf</div>
<div class="widget_content widget_content_1">sdfsdf</div>
<div class="widget_footer widget_footer_1">sdfsdfsdf</div>
</div>
<div class="widget widget_1">
<div class="widget_header widget_header_1">sdfsdf</div>
<div class="widget_sub_header widget_sub_header_1">ggggg</div>
<div class="widget_content widget_content_1">aaaaa</div>
<div class="widget_footer widget_footer_1">ssss</div>
</div>
<div class="widget widget_6">
<div class="widget_header widget_header_6">sssss</div>
<div class="widget_sub_header widget_sub_header_6">fgggg</div>
<div class="widget_content widget_content_6">fdff</div>
<div class="widget_footer widget_footer_6">dfgdfgdfg</div>
</div>
your jquery code works after with this change,
<script language="javascript" type="text/javascript" src="jquery-1.8.2.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$("div[class^='widget_content']").on({
mouseenter: function () {
alert("enter");
},
mouseleave: function () {
alert("leave");
}
});
});
</script>
来源:https://stackoverflow.com/questions/13266610/jquery-hover-effect-not-working