2017-08-17 8 views
1

無効なボタンの親にjQuery .click()リスナーの発火を防ぐ方法はありますか? 例えばこの場合:私は、有効/自体を無効にするか、ボタンへの結合を移動することはできませんので、私のプロジェクトのクリックイベントでIE:jQueryクリックリスナが無効なボタンの親に発生する

<div> 
    <button disabled="disabled">click me</button> 
</div> 
<script> 
    // unfortunately this click handler is placed by 3rd party 
    // library and I can't edit it. Just add another handlers or css 
    $('div').on('click', function() { 
    alert('div is still clickable, probable you use IE'); 
    }); 
</script> 

は、サードパーティのライブラリを置いています。

を追加:無効なボタンについて

$('button').on('click', function (event) { 
    if ($(this).hasClass('disabled')) { 
    event.stopImmediatePropagation(); 
    } 
}); 

が、何:私はボタンを無効にするが、それを「無効」クラスを証明し、イベントリスナーを追加しないと解決策を見つけましたか?それを扱う方法はありますか?

答えて

0

これはあなた

$('button').not(':disabled').parent().on('click',function(){}); 

のために働くかもしれないあなたは、イベントをクリックし、サードパーティを削除し、.off()またはunbind()

$('div').unbind(); //or .off() depending on how the third-party set the handler 
//then add your own click method after the unbinding. 
0

でも、ページ上でこのようにそれらをすべて再バインドできます。これを試してみてください。

$('div').on('click', function (event) { 
     if ($(this).find('button').is(':disabled')) { 
     return false; 
    } 
    }); 
+0

残念ながら、この新しいイベントリスナーは、すでにttachedなので、alert()の呼び出しを防ぎません。 – Tsenzuk

関連する問題