簡體   English   中英

Mouseenter Hover Div顯示/隱藏

[英]Mouseenter Hover Div Show/Hide

我正在嘗試使我的功能無法成功運行。 我需要做的就是能夠滾動到div上並顯示X,然后再添加所需的效果。

我正在使用Javascript,因為它需要跨瀏覽器兼容。 我不想在將來要添加的內容中使用CSS,因為它的功能非常有限。

<script>
$(document).ready(function() {
$('div.userinfo').hover({
    mouseenter: function() {
        $(this).children('div.delete').show();
    },
    mouseleave: function() {
        $(this).children('div.delete').hide();

    }
    });
    });
</script>
    <?
echo "<div class='userinfo'><div class='delete' style='cursor:pointer;position:relative;top:0px;float:right;padding-right:5px;' onclick=\"delete_('".$streamitem_data['streamitem_id']."');\">X</div></div>"

代替.hover()(已棄用並且無法像您嘗試使用它那樣工作),請使用.on():

$(function() {
    $('div.userinfo').on({
        mouseenter: function() {
            $(this).children('div.delete').show();
        },
        mouseleave: function() {
            $(this).children('div.delete').hide();

        }
    });
});

這樣就可以了。

使用bind()方法而不是將鼠標懸停:

$(document).ready(function() {
   $('div.userinfo').bind({
     mouseenter: function() {
        $(this).children('div.delete').show();
     },
     mouseleave: function() {
        $(this).children('div.delete').hide();
     }
   });
});

注意:如果以后要在jQuery中添加新的dom元素,請使用live()on() ,否則請使用bind方法,這是非常可靠的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM