找到具有特定文本的td,并在那之后立即对td进行操作?

我对jQuery选择器不太熟悉,所以想要一些帮助。

我有这样一张桌子:

Section Heading 1Section Text 1
Section Heading 2Section Text 2
Section Heading 3Section Text 3
Section Heading 4Section Text 4
Section Heading 5Section Text 5

我需要做的是找到包含特定文本的td ,并在右边的td对文本进行操作。

例如,假设我想找到包含文本Section Heading 4td ,并将td包含的文本连接到右边,文本为hello ,以便Section Text 4成为Section Text 4 hello ..

哪个jQuery选择器可用于此目的?

我的初步答案错过了关于更新文本的要点,这里有一个包含以下内容的示例:

 $(".test td:contains(Heading 1)") .next().text(function(){ return $(this).text() + " Hello" }); 

我不相信你可以使用append方法,因为我认为它只能添加html标签。

首先,您要使用:contains :

 jQuery( "td:contains(text)" ); 

然后,您可以使用.next和.append :

 jQuery( "td:contains(text)" ).next().append("Hello"); 

jQuery有:contains()伪类:

 $('td:contains("Section Heading 4")').next().text(function(_,t){ return t + ' Hello'; }) 

小心, :contains区分大小写。

这应该工作……

 $(function() { var searchText = "Section Heading 3"; var output = ""; $("td").each(function(i, item) { if($(item).html() == searchText) { output = $(item).html(); output += $(item).closest("td").html() } }); console.log(output); }); 

这是一个jsFiddle http://jsfiddle.net/5ELLM/

 var tds=$("table.test").find("td"); var td,i; for(i=0;i 

供参考

var data ="Search string" $('#table1 td:contains('+data+')').next().text();