我有一个html表:
Name Length Description x
Name1 444 Blabd x.png
Name2 55 Blabla x.png
Name3 11 Blaaa x.png
Run Code Online (Sandbox Code Playgroud)
table id="firsttable" img class="delete"
我有一个值(来自其他表):
var value = $(this).closest('tr').find('td:first').html();
Run Code Online (Sandbox Code Playgroud)
我想在表的第一列中找到该值并删除具有该值的行的x.png.
像:(有错误)
$('#firsttable tr td:first:contents("'+value+'")').closest('tr').find('td:last').remove();
Run Code Online (Sandbox Code Playgroud)
一些东西:
:first-child选择器而不是:first.:contains而不是:contents(我认为甚至没有:contents()选择器 - 或者可能通过插件)(参见注释).siblings()会找到当前元素的兄弟姐妹,这是第一个TD.你可以传递一个选择器来限制选择,所以传递:last给最后一个选择.比回到父母并找到最后一个TD更有意义.这是代码:
$('#firsttable td:first-child:contains("' + value + '")')
.siblings(':last')
.text('');
Run Code Online (Sandbox Code Playgroud)
注意:
:contains将在文本节点中搜索id包含的指定值,而不是确切的值!所以如果你这样做,:contains("1")但你的TD值为"14",它也会找到它.如果这是一个问题,你可以使用.filter():
$('#firsttable td:first-child').filter(function() {
return $(this).text() === value;
})
.siblings(':last')
.text('');
Run Code Online (Sandbox Code Playgroud)
更多信息: