我有这段代码:
<div class="content">
This is some title<br/>
Some text<br/>
And maybe more text.
</div>
我希望在第一句话之前和<span>
之前添加<br/>
这样的内容:
<div class="content">
<span>This is some title</span><br/>
Some text<br/>
And maybe more text.
</div>
答案 0 :(得分:4)
我建议:
$('.content br:first-child').each(function(){
var t = $(this.previousSibling).wrap('<span />');
});
虽然如果您只想设置第一行的样式,为什么不使用CSS的::first-line
伪元素:
div.content::first-line {
color: #f90;
font-size: 2em;
/* and so on, and so forth... */
}
参考文献: