2017-08-07 8 views
-1

私はhtmlでテーブルを持っています.jQueryやJavaScriptを使用して、テーブルの任意のセルでクリックイベントを無効にしたいとします(th, td, tr)。どうしたらいいですか?セルテーブルhtmlでクリックイベントを無効にするにはどうすればよいですか?

<table id="table1" border="1px"> 
 
<tr> 
 
    <th>Title1</th> 
 
    <td>Orange</td> 
 
    <th>Title2</th> 
 
    <td>Apple</td> 
 
</tr> 
 
<tr> 
 
    <th>Title3</th> 
 
    <td>Food</td> 
 
    <th>Title4</th> 
 
    <td>Fish</td> 
 
</tr> 
 
</table>

+0

すべての関連コードを含めてください – qiAlex

+1

CSSはおそらく? 'pointer-events:none'あなたが避けたいイベントハンドラをいつ、どのように付けているかを知るのに役立ちますが –

答えて

0

クリックイベントを無効にすることによって、あなたはイベントリスナーを無効にすることを意味している場合、off方法試してみてください。そうしないと

$(document).ready(function(){ 
 
    $('#disable-button').on('click',function(){ 
 
    $('#table1').off('click'); 
 
    }); 
 
    
 
    $('#table1').on('click',function(){ 
 
     alert('click event happened'); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="table1" border="1px"> 
 
<tr> 
 
    <th>Title1</th> 
 
    <td>Orange</td> 
 
    <th>Title2</th> 
 
    <td>Apple</td> 
 
</tr> 
 
<tr> 
 
    <th>Title3</th> 
 
    <td>Food</td> 
 
    <th>Title4</th> 
 
    <td>Fish</td> 
 
</tr> 
 
</table> 
 

 
<button id="disable-button">Disable listener</button>

、非常にシンプルに方法は、純粋なCSSを使用する:

pointer-events:none 

しかし、あなたはその代わりにjQueryでそれをやりたいので、:

をあなたは1.4.3+ jQueryのバージョンを使用している場合:

$('selector').click(false); 

ない場合:

$('selector').click(function(){ return false; }); 
0

<html> 
 
<head> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
<script> 
 
    
 

 
    $(document).ready(function(){ 
 
     $("#table1").click(function(e){ 
 
      if (e.target !== this) 
 
      return; 
 
      alert('clicked'); 
 
     }); 
 
    }); 
 
</script> 
 
</head> 
 
<body> 
 
    <table id="table1" border="1px"> 
 
    <tr> 
 
     <th>Title1</th> 
 
     <td>Orange</td> 
 
     <th>Title2</th> 
 
     <td>Apple</td> 
 
    </tr> 
 
    <tr> 
 
     <th>Title3</th> 
 
     <td>Food</td> 
 
     <th>Title4</th> 
 
     <td>Fish</td> 
 
    </tr> 
 
    </table> 
 
</body> 
 
</html>

0

さて、アフリカではコードにクリックイベントが登録されていないので、そのイベントがないとみなされます。 また、@ RoryMcCrossanがイベントを表示しないマウスについても述べました。またはcursor:default通常のカーソルアイコン(矢印)

関連する問題