我想自动设置页面上用引号括起来的所有单词的样式,但似乎无法用我的非常基本的jQuery技能弄明白。这是一个例子:
现在如何:
<p>"Hi, I'm Sietse", he said.</p>
我希望如何:
<p><strong>"Hi, I'm Sietse"</strong>, he said.</p>
任何线索?
我最近在这里问了一个类似的问题:Wrap different style to specific text between <li></li> before textual colon ":"?,但我似乎无法将这些方法用于这个新问题。 (由于某种原因,这个问题仍被标记为重复,但事实并非如此)
答案 0 :(得分:1)
使用正则表达式执行此操作
$('p').html(function(i,v){
return v.replace(/(".*")/g,'<strong>$1</strong>');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>"Hi, I'm Sietse", he said.</p>
&#13;
<强>更新强>
正如您在下面评论的那样,对于包含多个""
的复杂文本,请使用以下
$('p').html(function(i, v) {
return v.replace(/("[^"]*")/g, '<strong>$1</strong>');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>"Hi, I'm Sietse", he said, "and I'm from the Netherlands"</p>
&#13;