2016-03-25 9 views
0

クラス名をクリックして関数を書きました。それはうまく動作します。同じクラス名のタグを追加すると、そのクラスで関数が機能していません。関数が関数の文字列を追加していません

<script> 
    $(document).ready(function() { 

     $("#btn1").click(function(){ 
      $("p").append(" <b class='test'>Appended text</b>."); 
     }); 

     $(".test").click(function(){ 
      alert(12); 
     }); 

    }); 
</script> 

<p>This is a paragraph.</p> 
<p>This is another paragraph.</p> 

<ol> 
    <li class="test">List item 1</li> 
</ol> 

<button id="btn1">Append text</button> 
+0

[ 'イベントdelegation'](https://learn.jquery.com/events/event-delegation/)についての記事を読む – Rayon

+0

https://jsfiddle.net/rayon_1990/ dLgetr0w/ – Rayon

+0

最終的な解決策はここにあります:https://jsfiddle.net/dLgetr0w/1/ –

答えて

0

これは、jQueryが$(".test").click(...)に電話するときにイベントをバインドするためです。あなたは別にそれをバインドする必要があります。

$("#btn1").click(function(){ 
    $("p").append(" <b class='test'>Appended text</b>.") 
      .click(function(){alert(12);}); 
}); 
+0

「List item 1」をクリックしながら「12」を2回警告します。 – Neethu

+0

修正されました。しかし、私は重複した質問の解決策についていくつもりです。 –

関連する問題