0

要素をクリックすると、正常に動作する「mouseenter」および「mouseleave」イベントのバインドを解除したいのですが、別の要素がクリックされた場合はそれらを再度バインドしたいと考えています。これは機能しません。

助けはありますか?

コードは次のとおりです。

<script type="text/javascript">
  $(document).ready(function(){
        $("#shape1 img").click(function(){
          $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").unbind('mouseenter mouseleave');
    });

     $("#close").click(function(){
        $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").bind('mouseenter mouseleave');
     });
 });
</script>

どうもありがとう!

4

3 に答える 3

0

この.bind()関数では、これらのイベントがトリガーされたときに実行される関数を渡す必要があります。

$("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").bind('mouseenter mouseleave', function(event) {
    // do something here when the mouseenter or mouseleave events are triggered
});

イベントハンドラーを呼び出すと.unbind()、完全に削除され、jQuery はそれが何であったかを覚えていません。.bind()単にそれを元に戻すために呼び出すことはできず、それらのイベントに応答して実行する必要があるコードを認識させることはできません。

また、jQuery (1.7+) のバージョンによっては、イベント ハンドラーの追加と削除に関数.on()と関数を使用する必要があります。.off()

于 2013-01-30T11:07:38.627 に答える
0

イベントが発生したときに実行するコールバックを割り当てる必要があるためです。

試す :

<script type="text/javascript">
  $(document).ready(function(){
        var myFunctionMouseEnter = function(){
           alert('Hey');
        };
        var myFunctionMouseleave = function(){
           alert('Hey');
        };

        $("#shape1 img").click(function(){
          $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").off('mouseenter mouseleave');
        });

        $("#close").click(function(){
            $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").on('mouseenter',myFunctionMouseEnter )
                                                                               .on('mouseleave',myFunctionMouseleave );
        });
 });
</script>
于 2013-01-30T11:08:56.443 に答える
0

bind は、現在存在するアイテムのイベント ハンドラーのみをバインドします。

ドキュメントからBind()

ハンドラーは jQuery オブジェクトで現在選択されている要素にアタッチされるため、.bind() への呼び出しが発生した時点でそれらの要素が存在している必要があります。

On メソッドを使用します。

$("#shape1 img").click(function(){
          $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").off('mouseenter');
        });

        $("#close").click(function(){
            $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").on('mouseenter',myFunction1);
            $("#shape1 img,#shape2 img, #shape3 img, #shape4 img, #shape5 img").on('mouseleave',myFunction2);
        });
于 2013-01-30T11:09:18.483 に答える