简体   繁体   中英

How to ouput multi-line html with variable into div using javascript?

I want to output this multi line html with variable into div from inside jquery but it never ouput the block of code inside div! I am using only variable inside it which is"itemName". could any one tell me why my code doesn't out to div ?

var siteContents2 ="<li> +
"       <iframe src='<iframe src='http://bsite.com/itemshow.php?"+itemName+"&title=ok&bgcolor=white' height=200 width=200 style='border: none;></iframe><br>+
"        <div class="details">+
"        <div class="title">+
"          <a href="/+itemName/">+itemName</a>+
"        </div>+
"        </div>+
"    </li> ";   

document.getElementById("myDiv").innerHTML += siteContents2;


</script>   
</head>

<body>

    <div id="myDiv"></div>

Your quotes and concats are not correct. try this:

var siteContents2 ="<li>" + 
 "       <iframe src='<iframe src='http://bsite.com/itemshow.php?"+itemName+"&title=ok&bgcolor=white' height=200 width=200 style='border: none;></iframe><br> "+
"        <div class='details'>"+
"        <div class='title'>"+
"          <a href='/"+itemName+"/'>"+itemName+"</a>"+
"        </div>"+
"        </div>"+
"    </li> ";  

You're missing some quotes from the end of some of the lines and you can use either single quotes (') or an escaped double quote (\\") within the html.

var siteContents2 ="<li> "+
"       <iframe src='<iframe src='http://bsite.com/itemshow.php?"+itemName+"&title=ok&bgcolor=white' height=200 width=200 style='border: none;></iframe><br>"+
"        <div class='details'>"+
"        <div class='title'>"+
"          <a href='"+itemName+"'>"+itemName+"</a>"+
"        </div>"+
"        </div>"+
"    </li> ";   

document.getElementById("myDiv").innerHTML += siteContents2;

Your code had erroneous quotes throughout it so it was technically an invalid string and most likely being rejected. In addition, you can do multiline strings by ending each line with a \\ so you don't have to worry about so many quotes.

Your code, rewritten and working, could look like this:

var itemName = "blahblah";
var siteContents2 = "<li>\
       <iframe src='http://bsite.com/itemshow.php?" + itemName + "&title=ok&bgcolor=white' height=200 width=200 style='border: none;'></iframe>\
        <br>\
        <div class='details'>\
            <div class='title'>\
              <a href='" + itemName + "'>" + itemName + "</a>\
            </div>\
        </div>\
    </li>";

document.getElementById("myDiv").innerHTML += siteContents2;

Here's a jsFiddle of the code: http://jsfiddle.net/vkaC8/

Since ES6, you can store multi-line variables as template literals by surrounding them with backticks (`s):

var siteContents2 =`<li> 
       <iframe src='<iframe src='http://bsite.com/itemshow.php?`+itemName+`&title=ok&bgcolor=white' height=200 width=200 style='border: none;></iframe><br>
       <div class='details'>
       <div class='title'>
       <a href='`+itemName+`'>`+itemName+`</a>
    </div>
    </div>
</li> `; 
document.getElementById("myDiv").innerHTML += siteContents2;

More on template literals here .

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