簡體   English   中英

如何在插入表單jquery代碼的div上運行我的jquery代碼?

[英]How to run my jquery code on div that inserted form jquery code?

我需要你的代碼中的幫助我有問題當從jquery插入新div時插入的div不適用於我的jquery代碼如果有人有解決方案請告訴我這個我的Html代碼

<input type="text" id="t" value=""/>
<a href="#" class="btn">click</a><br/>
<br/>
<div  class="profileInfoSectionwall"style="border:1px solid #ccc">
text111111

    <div class="remove" style="display: none;">
        <a class="post_remove" rel="1" herf="#">X</a>
    </div>
</div>
<br/>
<div  class="profileInfoSectionwall"style="border:1px solid #ccc">
text222222

    <div class="remove" style="display: none;">
        <a class="post_remove" rel="2" herf="#">X</a>
    </div>
</div>

和這個jquery代碼

  $(document).ready(function(){
$(".profileInfoSectionwall").mouseenter(function(){
         $(this).children(".remove").show();

     });
     $(".profileInfoSectionwall").mouseleave(function(){
             $(".remove").hide();

         });

    $(".btn").click(function(){
         var s=$("#t").attr("value");
        $(this).after("<br/><br/><div class='profileInfoSectionwall' style='border:1px solid #ccc'>"+s+"<div class='remove' style='display: none;'><a class='post_remove' rel='3' herf='#'>X</a></div></div>");
    return  false;


    })



})

提前致謝

您可能想嘗試使用.on方法,如下所示:

$(".profileInfoSectionwall").on('mouseenter', function(){
     $(this).children(".remove").show();

});

$(".profileInfoSectionwall").on('mouseleave', function(){
    $(".remove").hide();
});

我創建了小提琴http://jsfiddle.net/mXBkV/1/

$(document).on('mouseenter', 'div.profileInfoSectionwall', function(){
    $(this).children(".remove").show();
});

$(document).on('mouseleave', 'div.profileInfoSectionwall', function(){
    $(".remove").hide();
});

實際上,在您的情況下,您需要的是live()方法,因為您希望處理當前現有OR未來DOM元素的事件(即:由JQuery創建)。

所以這是一個適合您的工作代碼:

http://jsfiddle.net/sidou/CMab3/

如果你想要一些改進,請告訴我

嘗試.live函數在jquery中生效

 $(".profileInfoSectionwall").live('mouseenter', function(){
    $(this).children(".remove").show();
});

$(".profileInfoSectionwall").live('mouseleave', function(){
    $(this).children(".remove").hide();
});

暫無
暫無

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

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