作者joechiboo (新的昵称)
看板Ajax
标题Re: [问题] jquery同时满足2个条件的列
时间Fri Jun 1 13:59:50 2012
※ 引述《saluyi (JOJO)》之铭言:
: 你是这个意思吗
: <script type="text/javascript">
: $(document).ready(function () {
: $("tr").each(function (i) {
: var t= $(this);
: if (t.find("td")[0].innerHTML == 0 && t.find("td")[2].innerHTML == 0) {
: t.attr("BGcolor", "red");
: }
: });
: });
: </script>
: <body>
: <table>
: <tr>
: <td>0</td>
: <td>2</td>
: <td>3</td>
: </tr>
: <tr>
: <td>4</td>
: <td>5</td>
: <td>6</td>
: </tr>
: <tr>
: <td>0</td>
: <td>8</td>
: <td>0</td>
: </tr>
: </table>
: </body>
: ※ 引述《SalmonFish (SF)》之铭言:
: : 我想用jquery取出同时满足2个条件的列
: : 例如一个第一栏与第3栏内容值都是0的列,然後整列标上红色
: : 分别抓我知道
: : $('tr td:nth-child(3)').text()==0
: : $('tr td:nth-child(1)').text()==0
: : 可是同时满足要怎麽取啊?
照你的逻辑写
<script type="text/javascript">
$(document).ready(function () {
$("tr").each(function (i) {
if ($(this).children('td:nth-child(3)').html() == 0 &&
$(this).children('td:nth-child(1)').html() == 0) {
$(this).attr("BGcolor", "red");
}
});
});
</script>
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.219.70.166
1F:→ no1kk:听说each的效能不好,可以用for loop 就用for吧 06/01 15:17
2F:→ TonyQ:以这个情况,each 只做 for 的 wrapper 不会效能不好啦。 06/01 15:37
4F:→ TonyQ:不会影响到哪去 06/01 15:37
5F:推 SalmonFish:感谢回覆&提供解法,但因被警告有效能上的ISSUE 06/01 20:21
6F:→ SalmonFish:所以最後还是采用上一篇saluyi大的作法了 06/01 20:22
7F:→ tyf99:上一篇不也用 each... 06/02 03:13
8F:→ SalmonFish:编译器警告的不是each...而是警告$(this)多次存取 06/03 02:45
9F:→ SalmonFish:而saluyi大用var t= $(this);就没有这个效能ISSUE 06/03 02:46
10F:推 s25g5d4:编译器? 06/03 19:00
11F:推 LaPass:哪一款? 06/03 20:07
12F:→ s25g5d4:我都不知道javascript有编译器...是指浏览器的警告? 06/03 20:15
13F:→ s25g5d4:还是这不是web... 06/03 20:16
14F:推 LaPass:是ide还是编译器? 06/03 22:53