2012-03-01 50 views
4

我需要你的幫助,我的代碼有問題,當從jQuery的 插入新的div插入DIV並不適用於我的jQuery代碼,如果任何人有解決辦法,請告訴我 這我的HTML代碼如何在插入表單jQuery代碼的div上運行我的jquery代碼?

<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; 


    }) 



}) 

在此先感謝

回答

0

嘗試.live功能live in jquery

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

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

從jQuery 1.7開始,不推薦使用.live()方法。使用.on()附加事件處理程序。老版本的jQuery用戶應優先使用.delegate(),而不要使用.live()。 http://api.jquery.com/live/ – arunes 2012-03-01 12:29:41

+1

@Mona Abdelmajeed感謝這個解決方案與我一起工作,非常感謝 – 2012-03-01 12:31:23

6

您可能需要使用該方法。對這樣類似嘗試:

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

}); 

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

感謝,但這個代碼不工作與我 – 2012-03-01 12:32:03

+0

沒有陰涼,它工作正常與我:),檢查你的jQuery版本 – 2012-03-01 12:39:33

3

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

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

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

事實上,你在你的情況需要的是現場()方法,你要處理的事件目前現有的或將來的DOM元素(即:由JQuery的創建) 。

因此,這裏是你一個工作代碼:

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

讓我知道如果你想要一些改進就可以了

+0

其實曲藝答案是最好的。儘管我在我的答案中提出了.live(),但我認爲最好是使用.on()方法來最佳實踐和代碼的穩定性,以防將來升級您的jQuery庫。所以,曲藝絕對值得最好的答案。 – Sidou 2012-03-01 12:45:10