使用jQuery在特定部分文本之前添加<br />

Ahm*_*lfy 4 html jquery

我有2个这样的段落:

<p>text bla bla bla owner of the idea : John Smith</p>
<p>text bla bla bla bla bla bla owner of the idea : James Marino</p>
Run Code Online (Sandbox Code Playgroud)

有没有什么方法可以使用jQuery在"想法的所有者"之前添加一个新行直到句末?

提前致谢

RRS*_*nov 6

如果这是jquery脚本的副本,请尝试使用\nfor new行.这是javascripts的工作.

jQuery WORKING示例:

 <p id="s">text bla bla bla owner of the idea : John Smith</p>
<p>text bla bla bla bla bla bla owner of the idea : James Marino</p>

<script type="text/javascript">
    $(document).ready(function() {
        $("p").each(function() {
            var getContent=$(this).text();
            var newString=getContent.replace('owner of the idea','<br />owner of the idea');
            $(this).html(newString);
        });
    });
</script>
Run Code Online (Sandbox Code Playgroud)


sma*_*man 5

如果模式一致,您可以根据:角色进行替换.

 $('p').each(function(){ 
          this.html(this.html().replace(":",":<br />"));
 }); 
Run Code Online (Sandbox Code Playgroud)

更新

根据评论,看起来我误解了这个问题.但是,您仍然可以使用相同的策略,

  var p = $('p:first');
  p.html(p.html().replace("owner of the idea", "<br />owner of the idea"));
Run Code Online (Sandbox Code Playgroud)