作者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