2012-03-01 3 views
4

私は と、この私の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> 

を教えてください、私は誰もが解決策を持っている場合、挿入されたdiv要素が私のjqueryのコードには適用されませんjqueryの から新しいdiv要素を挿入問題を抱えている私のコードでは、あなたの助けを必要とこのjqueryのコードあなたがそうのようなものを.onメソッドを使用して試してみたいことがあり、事前

答えて

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の古いバージョンのユーザーは、.live()よりも.delegate()を優先して使用する必要があります。 http://api.jquery.com/live/ – arunes

+1

@Mona Abdelmajeedさん、ありがとうございました。 –

6

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


    }) 



}) 

感謝:

あなたは現在、既存または将来のDOM要素(:jQueryのによって作成されたIE)上のイベントを処理するよう
$(".profileInfoSectionwall").on('mouseenter', function(){ 
    $(this).children(".remove").show(); 

}); 

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

感謝が、このコードが必要な場合は私に知らせて –

+0

私はうまく動作しません:)あなたのjqueryバージョンを確認してください –

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

を作成したが実際にあなたがあなたのケースで必要なものをライブ()メソッドであります。だからここ

はあなたのための作業コードです:

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

あなたはそれ

+0

実際にはアーネンの答えは最高です。私の答えでは.live()を提案しましたが、将来はjQueryライブラリをアップグレードする場合に備えて、コードのベストプラクティスと安定性のために.on()メソッドを使用する方が良いと思います。だから、アランは確かに最高の答えに値する。 – Sidou

関連する問題