2017-08-07 109 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的,而不是:如果您使用的jQuery版本1.4.3+

$('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

好吧,afaik,你还没有在代码中注册任何点击事件,所以它的考虑就好像你没有它们一样。 另外,@RoryMcCrossan还说,鼠标不显示任何事件。或cursor:default为正常光标图标(箭头)

相关问题