我想替换一些字符:[ngu1],[ngu2]到[chanquadi],并且我使用我的
<script type='text/javascript'>
var heo1 = document.querySelector(<.post-body>);
var heo2 = heo1.replace("[ngu1]", "chanquadi");
document.write(+ heo2 +);
</script>
但是它不起作用,请帮助我解决它。谢谢大家!
querySelector
似乎无效,请查看MDN docs。
而且您不能直接从Element
函数返回的querySelector
中进行替换。一种方法是获取元素的innerText
,然后进行替换。
最后,我不明白您想对document.write
语句做什么,因为它似乎也无效。
尝试一下是否可以解决您的问题:
var heo1 = document.querySelector('.post-body');
heo1.innerText = heo1.innerText.replace(/ngu1|ngu2/gi, "chanquadi");
<div class="post-body">
[ngu1] [ngu2] -- chanquadi
</div>
var heo1 = document.querySelector('.post-body');
heo1.innerText = heo1.innerText.replace(/ngu1|ngu2/gi, "chanquadi");
<div class="post-body">
[ngu1] [ngu2] -- chanquadi
</div>
[嗨,我要替换[ngu1],[ngu2]包括[],但不替换ngu1,ngu2。怎么做?