我必须根据某些业务逻辑更改表行颜色。我的访问权限有限。 我有决定 TD 内容的 PHP 代码...从该 TD 我必须更改当前的 TR 背景颜色。
jQuery 可以提供帮助,因为它有很多遍历器和访问器。
我正在编写的代码是
<script type="text/javascript">$(this).closest("td").css("border", "1px solid red");</script>
但无法访问当前的TD或TR
最佳答案
不要使用 this,而是给元素一个 id 并使用 jQuery 获取它。在您的代码中,这没有任何意义。像:
<table><tr><td> <input id="myInput" type="text" /> </td></tr></table>
//If you want to access the td
$('#myInput').closest("td").css("border", "1px solid red");
//If you want to access the tr
$('#myInput').closest("tr").css("background", "red");
关于javascript - 需要访问当前TR或TD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13800040/