gpt4 book ai didi

jquery - 如何为通过 contains() 搜索的文本添加下划线

转载 作者:行者123 更新时间:2023-12-01 00:08:20 25 4
gpt4 key购买 nike

<div>John Resig</div>
<div>George Martin</div>
<div>Malcom John Sinclair</div>
<div>J. Ohn</div>
<script>
$("div:contains('John')").css("text-decoration", "underline");
</script>

上面的代码将为包含“John”的总DIV添加下划线。但是我怎样才能只让“John”带有下划线。
请有人帮助我。

最佳答案

您必须将文本包裹在一个跨度中:

$("div:contains('John')").each(function()
{
var $el = $(this);
$el.html( $el.html().replace(/John/g, '<span style="text-decoration: underline">John</span>') );
});

http://jsfiddle.net/GP96T/2/

正如 @Brad Christie 所指出的,只有当您知道属性中没有“John”,或者 div 没有子项时,才应该使用此选项。

关于jquery - 如何为通过 contains() 搜索的文本添加下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7162494/

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