jQuery hover effect not working

六月ゝ 毕业季﹏ 提交于 2019-12-12 00:49:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!