我是jQuery的新手。
我正在开设一个筹款活动网站,其中包含一份赞助商名单。
表数据由名字(.col2),姓氏(.col3)和匿名(.col5)组成。
看起来像这样:
<table>
<tbody>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Anonymous</th>
</tr>
<tr>
<td class="col2">John</td>
<td class="col3">Smith</td>
<td class="col5">Y</td>
</tr>
<tr>
<td class="col2">Jane</td>
<td class="col3">Doe</td>
<td class="col5">N</td>
</tr>
<tbody>
</table>
如何用&#34; Anonymous&#34;替换.col2和.col3。如果.col5 td值为&#34; Y&#34;?
答案 0 :(得分:0)
尝试
$('td.col5').filter(function () {
return $.trim($(this).text()) === 'N';
}).prev().text('Anonymous').prev().text('Anonymous');
<小时/> 或
$('td.col5').filter(function () {
return $.trim($(this).text()) === 'N';
}).closest('tr').find('.col2,.col3').text('Anonymous');
或
$('td.col5').filter(function () {
return $.trim($(this).text()) === 'N';
}).parent().children('.col2,.col3').text('Anonymous');
答案 1 :(得分:0)
尝试this
$(".col5:contains('Y'), .col5:contains('y')").each(function(){
var par = $(this).parent();
$(par).find(".col2, .col3").text("Anonymous");
});