使用jquery在标记内插入text.before()

使用jquery在标记内插入text.before(),jquery,insert,Jquery,Insert,下面是我尝试做的一个基本示例: <div class="container"> <h2>Greetings</h2> <p>All this content, including the HTML comes from a CMS</p> </div> 那么如何将其插入标签中呢?我不能用类标记该p,也不能在标记内放置标记,因为它是从CMS吐出的。使用text() 我找到了prepend(),它似乎符合我的要求。这也

下面是我尝试做的一个基本示例:

<div class="container">
  <h2>Greetings</h2>
  <p>All this content, including the HTML comes from a CMS</p>
</div>
那么如何将其插入标签中呢?我不能用类标记该p,也不能在标记内放置标记,因为它是从CMS吐出的。

使用text()


我找到了prepend(),它似乎符合我的要求。这也是另一个解决方案。谢谢

你为什么不用div标记来包装你的p标记,也许你可以更容易地处理它呢?正确,用这样的东西-$(“p”)。prepend(“纽约-2010年12月29日-”;
<div class="container">
  <h2>Greetings</h2>
  <p>NEW YORK - December 29, 2010 - All this content, including the HTML comes from a CMS</p>
</div>
$('.container p').before('NEW YORK - December 29, 2010');
var oldText = $('.container p').text();
$('.container p').text("NEW YORK - December 29: " + oldtext);
$('.container p').text(function(i,v) {
    return "NEW YORK - December 29: " + v;
});