简体   繁体   English

突出显示表格中的选定行

[英]Highlighting the selected row in a table

I am having a table in a html. 我在html中有一个表格。

<table>
  <tr>
    <td>
     <button onclick="someFunction()">
    </td>
    <td>data</td>
  </tr>
  <tr>
    <td>
     <button onclick="someFunction()">
    </td>
    <td>data</td>
  </tr>
  <tr>
    <td>
     <button onclick="someFunction()">
    </td>
    <td>data</td>
  </tr>
</table>

Each row having a dynamic id ,Here is my question I want highlight the current row of the table which button been pressed . 每行都有一个动态ID,这是我的问题, 我想突出显示表的当前行中按下了哪个按钮

How can I achieve it? 我该如何实现?

A good approach will be is to change your markup like 一个好的方法是像

<table id="mytable">
    <tr>
        <td><button>click</button></td>
        <td>data</td>
    </tr>
    <tr>
        <td><button>click</button></td>
        <td>data</td>
    </tr>
    <tr>
        <td><button>click</button></td>
        <td>data</td>
    </tr>
</table>

and

#mytable tr.highlight{
    background-color: yellow;
}

then 然后

$('#mytable button').click(function(){
    $('#mytable tr.highlight').removeClass('highlight');
    $(this).closest('tr').addClass('highlight');
    someFucntion();
})

Demo: Fiddle 演示: 小提琴

Use closest() in jquery to select the parent <tr> . 在jquery中使用closest()来选择父<tr>

HTML HTML

<tr><td><button onclick="someFucntion(this)"></td><td>data</td></tr>

JS JS

function someFunction(elm){
 $("tr.highLightClass").removeClass("highLightClass");// remove class if it is having someone
$(elm).closest("tr").addClass("highLightClass"); // here selected row

}

DEMO DEMO

If you want to use "best practice", remove all inline handlers. 如果要使用“最佳实践”,请删除所有内联处理程序。 Try this: 尝试这个:

$("button.someClass").click(function () {
    $("tr.highLightClass").removeClass("highLightClass"); // remove class if it is having someone
    $(this).closest("tr").addClass("highLightClass"); // here selected row

});

DEMO DEMO

Onclick is outdated and is a very bad practice.. Add a class / id to selector.. Onclick已过时,这是一个非常不好的做法。.将一个类/ id添加到选择器。

<table>
<tr><td><button class="clickMe"></td><td>data</td></tr>
</table>

JavaScript : JavaScript:

$('body').on("click", ".clickMe", function(){
    var that = this;
    that.closest("tr").addClass("highlightMe");
});

Why use on? 为什么要用在?

If your table is being generated dynamically or rows are being added on the go, you should watch the new dynamic elements as well.. 如果您的表是动态生成的,或者正在行中添加行,则还应该注意新的动态元素。

From your question I understand the following: when you click the button inside the you want the corresponding <tr> to be highlighted. 从您的问题中我了解以下内容:当您单击内部的按钮时,您想要突出显示相应的<tr>

Here's a JSFiddle demonstrating my answer: http://jsfiddle.net/WBison/2vkfn5uy/2/ 这是一个JSFiddle演示我的答案: http : //jsfiddle.net/WBison/2vkfn5uy/2/

Basically, use the event object to retrieve the target node (the button), then the parent of that (the <td> ), then the parent of THAT (the <tr> ). 基本上,使用事件对象检索目标节点(按钮),然后检索该目标节点的父节点( <td> ),然后检索THAT的父节点( <tr> )。 Then just use any method you'd like to change the style. 然后,只需使用您想要更改样式的任何方法即可。

Code: 码:

Button function call: <button onclick="someFunction(event)"> 按钮函数调用: <button onclick="someFunction(event)">

function someFunction(e) {
    e.target.parentNode.parentNode.style.backgroundColor = "blue";
}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM