1

このコードを実行すると、alertt()関数に対してアラートが何度もポップアップします。

<script>
      var newdiv = '<div id="text2" onmouseover="alertt()" contenteditable="true">text color to be changed</div>';
    $(document).ready(function(){
         $('.addbutton').click(function () {
             $(this).after($(newdiv).addClass('e'));
         });
        $(".e").draggable({ }) .click(function() { 
              $(this).draggable({disabled: true}); })
                  .blur(function() { $(this).draggable({disabled: false});
                  alert('2');
                  });
    });
    function alertt() { 
        $(".e").draggable({ })
        .click(function() { 
            $(this).draggable({disabled: true});
            alert('3');
        })
        .blur(function() {
            $(this).draggable({disabled: false});
            alert('4');
        });
    }
      </script>
    <body>

    <div id="output"></div>
    <button class="addbutton">Add Text</button>
    <div id="text" class="e" contenteditable="true">text color to be changed</div>
    </body>

そして、なぜこの関数が新しい要素に対して機能しないのです<div>addClass('e')?

$(".e").draggable({ }) .click(function() { 
                  $(this).draggable({disabled: true}); })
                      .blur(function() { $(this).draggable({disabled: false});
                      alert('2');
                      });

これを解決するための提案や修正に感謝します。

4

1 に答える 1

0

それらは動的に作成され、クリック イベントが関連付けられていないため、機能しません。newDivを作成した後、クリックイベントマニュアルを添付する必要があります。または、すべての '.e' からクリック イベントのバインドを解除し、すべてに再度バインドします。

于 2013-10-26T08:10:38.713 に答える