admin管理员组文章数量:1391955
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
4 Answers
Reset to default 3Since 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/
本文标签: How to ouput multiline html with variable into div using javascriptStack Overflow
版权声明:本文标题:How to ouput multi-line html with variable into div using javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744733272a2622172.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论