开发者

Adding <br /> before specific part of text using jQuery

I have 2 paragraphs like this :

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

Is there any way I could use jQuery to add a new line before "Owner of the idea" till t开发者_StackOverflow中文版he end of the sentence?

Thanks in advance


If this is a copy of your jquery script, try using a \n for new line. That work in javascripts.

jQuery WORKING example:

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


If the pattern is consistent, you could do a replace based on the : character.

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

Update

According to the comment, it looks like I misread the question. You can still use this same strategy however,

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


$('<br />').insertBefore(selector for your paragraph);

I don't know what your surrounding markup looks like, so it's not clear what the best selector for your paragraph would be.


$('p').each(function() {
    $(this).html($(this).text().replace(/\bowner of the idea/i,'<br/>$&'));
});


Put a class on the break then show and hide at will, like so:

html:

<br class="break" style="display:none;"/>

jquery:

$('.break').show();
$('.break').hide();


You simply need to prepend the <br /> to the paragraph using this JavaScript:

$("p").prepend("<br />");

Update I see what you mean now... Try this code:

var old=$("p").html();
$("p").html(old.replace(/owner/gi,"<br />owner");

Ad@m

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜