gpt4 book ai didi

Jquery包含文本并将其设为蓝色

转载 作者:行者123 更新时间:2023-12-01 00:16:17 24 4
gpt4 key购买 nike

设计师希望将公司名称设为蓝色,但公司名称嵌入在文本中,周围没有任何 html 元素。

我希望能够找到一个字符串并仅将该字符串设置为蓝色

我已经有了这段代码,但它使整个文本变成蓝色

$(function() {
$('div:contains("The Company Name")').css("color","blue");
});

这是示例代码

<div>The company name is great and makes a lot of happy customers</div>
<div>You can have more info on <a href="link">The company name</a>[...]</div>

这就是我想要的结果

<div><span style="color:blue;">The company name</span> is great and makes a lot of happy customers</div>
<div>You can have more info on <a href="link"><span style="color:blue;">The company name</span></a>[...]</div>

有人吗?

最佳答案

    $(function() {
$('div').each(function() {
$(this).html($(this).html().toString().replace('The Company Name', '<span style="color: blue">The Company Name</span>'));
});
});

关于Jquery包含文本并将其设为蓝色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6389506/

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