在第一句中添加元素

时间:2013-10-04 12:45:42

标签: jquery css

我有这段代码:

<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>

1 个答案:

答案 0 :(得分:4)

我建议:

$('.content br:first-child').each(function(){
    var t = $(this.previousSibling).wrap('<span />');
});

JS Fiddle demo

虽然如果您只想设置第一行的样式,为什么不使用CSS的::first-line伪元素:

div.content::first-line {
    color: #f90;
    font-size: 2em;
    /* and so on, and so forth... */
}

JS Fiddle demo

参考文献: