Embed HTML in JavaScript - Stack Overflow

I have a chunk of HTML which I'd like to add to a document at run time.How do I embed the HTML wit

I have a chunk of HTML which I'd like to add to a document at run time.

How do I embed the HTML within the JS file?

var p = document.createElement("div");
p.innerHTML = '<div>  MY HTML  
               <some element>  </some element>
               <some element>  </some element>
               </div>'

The problem is that my HTML is multiline; how do I add the newline chars within a JS string?

I have a chunk of HTML which I'd like to add to a document at run time.

How do I embed the HTML within the JS file?

var p = document.createElement("div");
p.innerHTML = '<div>  MY HTML  
               <some element>  </some element>
               <some element>  </some element>
               </div>'

The problem is that my HTML is multiline; how do I add the newline chars within a JS string?

Share asked Apr 30, 2009 at 17:08 Robin RodricksRobin Rodricks 114k147 gold badges414 silver badges617 bronze badges 3
  • You don't want the <br/> tags do you? – dirkgently Commented Apr 30, 2009 at 17:11
  • 4 Why do you care if your HTML is multiline? The browser will be stripping out the whitespace anyway. – Thomas L Holaday Commented Apr 30, 2009 at 17:12
  • 3 @Thomas: Ever tried editing a large chunk of HTML (or anything else for that matter) that is place all on one line. – AnthonyWJones Commented Apr 30, 2009 at 17:19
Add a ment  | 

1 Answer 1

Reset to default 2

you can use backslash character to ignore end of line in JavaScript strings.

http://www.ponentart./BLOGS/jovan/archive/2007/10/17/multiline-strings-in-javascript.aspx

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

相关推荐

  • Embed HTML in JavaScript - Stack Overflow

    I have a chunk of HTML which I'd like to add to a document at run time.How do I embed the HTML wit

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信