替换不起作用

时间:2015-07-23 09:46:24

标签: jquery html

var full_text=$(".landingpage .tableCol-33:nth-child(3) tbody:nth-child(even) tr td").text();
//The above will give me "Subcategory:Mutual Funds"

var sub_text=$(".landingpage .tableCol-33:nth-child(3) tbody:nth-child(even) tr td a").text();
//The above will give "Subcategory"

var next_text=$(".landingpage .tableCol-33:nth-child(3) tbody:nth-child(even) tr td").text().substring(14,30);
//The above will give "Mutual Funds"

我想将Subcategory替换为Mutual Funds。所以我使用了下面的代码,但它没有用.KIndly引导我同样的。

var output=full_text.replace(sub_text,next_text);

1 个答案:

答案 0 :(得分:2)

您只是在代码中更改文本,而不是在页面上元素所在的DOM中。您需要使用text()方法更新DOM中的新文字。

请参阅代码中的内联注释:

var full_text = $(".landingpage .tableCol-33:nth-child(3) tbody:nth-child(even) tr td");
// full_text is now jQuery object

var sub_text = $(".landingpage .tableCol-33:nth-child(3) tbody:nth-child(even) tr td a").text();
var next_text = $(".landingpage .tableCol-33:nth-child(3) tbody:nth-child(even) tr td").text().substring(14, 30);

full_text.text(full_text.text().replace(sub_text, next_text));
// Update the innerText of full_text after replacing the text.