Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何在jQuery中用html标签替换字符串的一部分? 说例如<div>Who am i</div>应该是<div><b>Who</b> am i</div>。
<div>Who am i</div>
<div><b>Who</b> am i</div>
您可以使用html方法的回调函数和replace方法。
html
replace
$('div').html(function(_, oldHTML){ return oldHTML.replace(/(\w+)/, '<b>$1</b>'); // return oldHTML.replace('Who', '<b>Who</b>'); })
http://jsfiddle.net/3rAMP/
.html()jquery的使用
.html()
$("div").html("<b>Who</b> am i")