简体   繁体   中英

Find and replace in div tag using jQuery?

I have div tag with css class name. div class name is same in for other div tags. I want findout a text inside the div tag with css class name and replace with next text

  <div class="ms-listdescription">            
 Share a document with the team by adding it to this document library.
  </div>

with new text

 <div class="ms-listdescription">             
      Share a document with the team by adding it to this document library 
           <br/> new text.
    </div>

Update: This is very simple just find text "Share a document with the team by adding it to this document library" in if it is matches text then only append new text with existing text.

var txt = $("div.ms-listdescription").html();
if( txt == "Share a document with the team by adding it to this document library" ) {
 $("div.ms-listdescription").html( txt + "<br/>new text" );
} 

see above -- is this what you want?

I am not sure I completly understand your question but I think you want the jquery .append() function

your code would look like this

$('.ms-listdescription').append('<br/> NEWTEXT');

This is better than the . html() because it does not clear the inside it just adds to the end of the stuff in the div.

You can check that it contains text by using :contains() .

$(".ms-listdescription:contains('Share a document with the team by adding it to this document library')")
    .html(
        $(".ms-listdescription").html() + $("<br>new text")
    );
$('.ms-listdescription').each(function(){
    if ($(this).html() === "Share a document with the team by adding it to this document library") {
        $(this).append('<br/>new text');
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM