在我用言语试图解释我的意思之前,这里有一个我想要实现的目标的示例。
现在怎么样:
<ul>
<li>My name: Sietse</li>
<li>Country: The Netherlands</li>
</ul>
我想要的样子:
<ul>
<li><strong>My name:</strong> Sietse</li>
<li><strong>Country:</strong> The Netherlands</li>
</ul>
我的网站上有很多现有内容的标记与第一个示例类似,但是有没有一种方法可以动态选择文本并设置文本样式,直到 LI 中的冒号(可能包括),如第二个例子?
请注意,我绝对是 jQuery 或任何 Javascript 方面的初学者。
最佳答案
$('ul li').html(function (i, html) {
return html.replace(/(.*?:)/, '<strong>$1</strong>')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
<li>My name: Sietse</li>
<li>Country: The Netherlands</li>
</ul>
关于jquery - 在文本冒号 ":"之前的 <li></li> 之间将不同的样式包装到特定文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26480530/