简体   繁体   中英

Jquery: add html to text

I have some jquery that I am using to insert some text based on an IF statement, this works perfectly but I need to now add a new class to this text to style it, but when I do this it does not render anything.

Existing and working script.

      $(document).ready(function(){
        if($('.productdetail').length){
        }else{
       document.getElementById('listwrapper').innerHTML = "Sorry! Your search found no products, please adjust your filters. If you need help call (02) 9620 2424."
         } 
      });

What I did and broke it:

      $(document).ready(function(){
        if($('.productdetail').length){
        }else{
       document.getElementById('listwrapper').innerHTML = "<span class='sorry'>Sorry! Your search found no products, please adjust your filters. If you need help call (02) 9620 2424.</span>"
         } 
      });

Any advice or help would be awesome!

Use .html() in jquery

$(document).ready(function(){
        if($('.productdetail').length){
        }
        else{
              $('#listwrapper').html("<span class='sorry'>Sorry! Your search found no products, please adjust your filters. If you need help call (02) 9620 2424.</span>");
         } 
 });

Use like this.

 $(document).ready(function(){
    if($('.productdetail').length){
    }else{
   document.getElementById('listwrapper').html("<span class='sorry'>Sorry! Your search found no products, please adjust your filters. If you need help call (02) 9620 2424.</span>");}
  });

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