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

I want to output this multi line html with variable into div from inside jquery but it never ouput the

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='.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>

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./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>
Share Improve this question asked May 10, 2013 at 8:35 user1788736user1788736 2,84521 gold badges68 silver badges118 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

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

Your quotes and concats are not correct. try this:

var siteContents2 ="<li>" + 
 "       <iframe src='<iframe src='http://bsite./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./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./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/vkaC8/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744733273a4590590.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信