admin管理员组文章数量:1399116
I have some html code that i want to add dynamically to a div in a page.
My div is:
<div id="my_div" class="my_class">
</div>
This is my html code that i want to add to the above div, when i click on some button, using jquery onclick event.
<div id="content">
'Hello world 123' <br>
<img src='player.gif' width='200' height='100'>
Good morning...
Have a nice day...
</div>
So far, i tried this general way, when onclick event is invoked:
document.getElementById("my_div").innerHTML =
'<div id="content">
'Hello world 123' <br>
<img src='player.gif' width='200' height='100'>
Good morning...
Have a nice day...
</div>';
But as you can see, the code is error prone, as the quotes are mismatched and it breaks. How can i escape the quotes and add the content to div efficiently?
I have some html code that i want to add dynamically to a div in a page.
My div is:
<div id="my_div" class="my_class">
</div>
This is my html code that i want to add to the above div, when i click on some button, using jquery onclick event.
<div id="content">
'Hello world 123' <br>
<img src='player.gif' width='200' height='100'>
Good morning...
Have a nice day...
</div>
So far, i tried this general way, when onclick event is invoked:
document.getElementById("my_div").innerHTML =
'<div id="content">
'Hello world 123' <br>
<img src='player.gif' width='200' height='100'>
Good morning...
Have a nice day...
</div>';
But as you can see, the code is error prone, as the quotes are mismatched and it breaks. How can i escape the quotes and add the content to div efficiently?
Share Improve this question asked Jul 11, 2011 at 5:47 shasi kanthshasi kanth 7,10226 gold badges111 silver badges164 bronze badges4 Answers
Reset to default 5this should do the job:
document.getElementById("my_div").innerHTML =
"<div id=\"content\">
'Hello world 123' <br>
<img src='player.gif' width='200' height='100'>
Good morning...
Have a nice day...
</div>";
Instead i have escaped the double quotes around content.
You can just use backslash to escape it:
document.getElementById("my_div").innerHTML =
'<div id="content">
\'Hello world 123\' <br>
<img src=\'player.gif\' width=\'200\' height=\'100\'>
Good morning...
Have a nice day...
</div>';
Try this:
document.getElementById("my_div").innerHTML =
'<div id="content">
\'Hello world 123\' <br>
<img src="player.gif" width="200" height="100">
Good morning...
Have a nice day...
</div>';
With a simple example
http://jsfiddle/arjunshetty2020/MPYXS/
$(document).ready(function() {
$("#btn").click(function() {
document.getElementById("my_div").innerHTML ="<p id=\"test\">\"test1\" <br/> test2<p>" ;
});
});
本文标签: javascriptHow to add this html in a divescaping the quotesStack Overflow
版权声明:本文标题:javascript - How to add this html in a div, escaping the quotes? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744201276a2594972.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论