在jquery中替换span中的文本

时间:2017-03-25 11:20:56

标签: jquery

我有以下范围

<span class="base" data-ui-id="page-title-wrapper">Search results for: 'soda'</span>

我想删除Search results for并替换为其他文字

这是jquery

$(".base span").text(function(index, text) {

return text.replace('Search results for', 'We got you');

});

这是jsfiddle https://jsfiddle.net/kcLuh0a1/1/

为什么代码不会改变文本?

2 个答案:

答案 0 :(得分:0)

您的问题是找不到.base span。请改用span.base

$("span.base").text(function(index, text) {
    return text.replace('Search results for', 'We got you');
});

JSFiddle:https://jsfiddle.net/y3a98pde/

答案 1 :(得分:0)

根据您的代码,您在元素span

中找到base元素

但是你需要将它应用于span,所以你的代码就像这样

$("span.base").text(function(index, text) {

return text.replace('Search results for', 'We got you');

});