gpt4 book ai didi

javascript - 使用 jQuery 查找和替换 HTML 字符串

转载 作者:可可西里 更新时间:2023-11-01 02:33:45 24 4
gpt4 key购买 nike

数据库中的链接是网站标题并在页面“Interesting Article:作者”上呈现 - 但偶尔链接是一个问题“Where's China?:GoogleMaps”。 ?:看起来很傻所以我想替换 H​​TML ?</span>:?</span> .

这是我编写的 jQuery:

$('#relatedinfo ul li a').html().replace('?</span>:','?</span>');

但这实际上并没有在 DOM 中替换它。如何获取该字符串以实际更改页面?

最佳答案

我建议:

$('#relatedinfo ul li a').html(function(index,html){
return html.replace(/<\/span>(\:)/,'');
});

JS Fiddle demo .

甚至:

$('#relatedinfo ul li a').text(function(index,text){
return text.replace(':','');
});

JS Fiddle demo .

一种更新的方法是检查 span 中的最后一个字符是否在 ['?','!','.'] 的数组中,并且,如果是,则从 nextSibling 的节点值中删除 ::

$('#relatedinfo ul li a span').text(function(index,text){
var lastchar = text.split('').pop();
if (['?','!','.'].indexOf(lastchar) > -1) {
this.nextSibling.nodeValue = this.nextSibling.nodeValue.replace(':','');
}
});

JS Fiddle demo .

引用资料:

关于javascript - 使用 jQuery 查找和替换 HTML 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15203611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com