gpt4 book ai didi

javascript - 如何仅更改链接内的文本

转载 作者:行者123 更新时间:2023-12-02 16:05:23 25 4
gpt4 key购买 nike

我有以下链接,我想将其文本从“新子网站”修改为“创建新客户网站”:-

enter image description here

所以我写了以下 jquery :-

$('a#createnewsite').text('Create New Customer Site');

它不仅替换了文本,还替换了包含“+”图像的范围,而我试图保留除替换文本之外的所有内容。这是应用 jQuery 后的结果:-

enter image description here

最佳答案

可以使用 jQuery.contents 选择文本节点与 jQuery.filter 一起:

$(function() {
$("a#createnewsite").contents().filter(function() {
// filter text nodes that contain one or more non-whitespace characters
return this.nodeType === Node.TEXT_NODE && /\S/.test(this.nodeValue);
}).replaceWith("new text");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<a id="createnewsite" href="#">
<span>
<img src="http://dummyimage.com/64x64/000/fff" width="64" height="64">
</span>
new subsite
</a>

关于javascript - 如何仅更改链接内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30805532/

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