[英]JQuery click event works only once in a <tr>
how can I make this click just work once in my html? 我如何才能使此点击在我的html中只起作用一次?
right now it is working in all tr from the table, should work just once after this not anymore. 现在,它在表中的所有tr中都有效,此后应该只工作一次。
$("#example tbody tr").on("click", function() {
$(this).addClass('row-selected');
})
You will need to unbind the click event after your first click: 首次单击后,您需要取消绑定click事件:
var tr = $("tr"); tr.one("click", function() { // this means that you can only click each row once $(this).toggleClass('row-selected'); tr.off('click'); // this will unbind the click event from the rest of the rows })
.row-selected td { font-weight: bold; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>one</td> </tr> <tr> <td>one</td> </tr> <tr> <td>one</td> </tr> <tr> <td>one</td> </tr> <tr> <td>one</td> </tr> <tr> <td>one</td> </tr> </table>
Try with one
instead of on
. 试着用
one
,而不是on
。 more about one
http://api.jquery.com/one/ 有关
one
http://api.jquery.com/one/的更多信息
("#example tbody tr").one("click", function() {
$(this).addClass('row-selected');
})
Try this. 尝试这个。
$("#example tbody tr").one("click", function() {
$(this).addClass('row-selected');
})
Inside your click event callback you can remove the event. 在click事件回调中,您可以删除事件。 Just pass the event through as a parameter.
只需将事件作为参数传递即可。
$("#example tbody tr").on("click", function(event) {
$(this).addClass('row-selected');
event.target.onclick = null;
})
// Try This
jQuery(document).ready(function(){
$(document.body).on("click", '#example tbody tr', function() {
$(this).addClass('row-selected');
});
});
Try changing the on("click" to one("click" 尝试将on(“ click”更改为one(“ click”
Try the below 试试下面
$("#example tbody tr").one("click", function() {
$(this).addClass('row-selected');
})code here
or you can try checking out this page bind unbind 或者您可以尝试签出此页面绑定取消绑定
Here is the example they use on that site I linked. 这是他们在我链接的该网站上使用的示例。
$('#my-selector').bind('click', function() {
$(this).unbind('click');
alert('Clicked and unbound!');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.