Javascript 用于在表格行上获取 Click 事件的 jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6816981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:19:22  来源:igfitidea点击:

jQuery for getting Click event on a Table row

javascriptjquery

提问by Nithesh Narayanan

I have the following table

我有下表

<table>
<tr class="rows"><td>cell1</td><td>cell2</td></tr>
</table>

How can i set an alert message if i clicked on any of the column of <tr class="rows">using jquery?

如果我单击any of the column of <tr class="rows">使用 jquery,如何设置警报消息?

回答by ShankarSangoli

You can use delegate for better performance which will attach click event to root container of rows i.e table

您可以使用委托以获得更好的性能,它将单击事件附加到行的根容器,即表

$(document).ready(function(){
    $("tableSelector").delegate("tr.rows", "click", function(){
        alert("Click!");
    });
});

回答by epascarello

$(
  function(){
      $(".rows").click(
        function(e){
            alert("Clicked on row");
            alert(e.target.innerHTML);
        }
      )
  }
)

Example

例子

Better solution

更好的解决方案

$(document).on("click","tr.rows td", function(e){
    alert(e.target.innerHTML);
});

回答by Fender

$(document).ready(function(){
    $("tr.rows").click(function(){
        alert("Click!");
    });
});

回答by Svetlin Panayotov

$(".rows").click(function (){ 
   alert('click');
});