gpt4 book ai didi

javascript - 有没有办法将类添加到特定类的第一个单词?

转载 作者:行者123 更新时间:2023-11-30 11:55:30 26 4
gpt4 key购买 nike

我正在寻找一种方法来将自定义类添加到特定类的第一个和第二个单词?

我的代码是这样的

<h2 class="content-heading">Latest News</h2>

我想有类似的东西

<h2 class="content-heading"><span class="1">Latest</span> <span class="2">News</span></h2>

最佳答案

您可以使用函数设置 html,然后将 innerTextmap 拆分跨度

$('.content-heading').html(function() {
return this.innerText.split(' ').map(function(e, i) {
return $('<span />', {
class: 'i' + i,
text: e
});
});
});
.i0 {
color:red;
}

.i1{
color:blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h2 class="content-heading">Latest News</h2>

注意:类必须是 SGML 名称。因此,我在示例中使用了 i 前缀。

关于javascript - 有没有办法将类添加到特定类的第一个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38108626/

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