Is it possible to include HTML code in JavaScript?

后端 未结 5 475
伪装坚强ぢ
伪装坚强ぢ 2021-02-04 14:32

I have a basic question, can i include HTML code in JS? (with \"document.write\")

This is my HTML code:

    
  • 相关标签:
    5条回答
    • 2021-02-04 15:07

      You can do:

      document.getElementById("menu").innerHTML="<div>hello</div>"
      
      0 讨论(0)
    • 2021-02-04 15:10
      window.onload = function(){
          document.getElementById("menu").innerHTML='<li class="topline"><a href="#">some text</a></li><li><a href="#">some text </a></li><li><a href="#">some text</a></li><li><a href="#">some text</a></li><li><a href="#"some text</a></li><li><a href="#">some text</a></li><li><a href="#">some text</a></li>';
      }
      

      Edited:

      It should work.

      <script type="text/javascript">
      window.onload = function(){
          document.body.innerHTML = 
              '<li><a href="#" class="menulink">text</a></li>'
              +'<li><a href="#" class="menulink">text</a></li>'
              +'<li><a href="#" class="menulink">text</a><ul>'
              +'<li><a href="#" class="sub">text</a><ul>'
              +'<li class="topline"><a href="#">text</a></li>'
              +'<li><a href="#">text </a></li><li><a href="#">text</a></li>'
              +'<li><a href="#">text</a></li><li><a href="#">text</a></li>'
              +'<li><a href="#"text</a></li><li><a href="#">text</a></li></ul>'
              +'</li><li><a href="#" class="sub">text </a><ul>'
              +'<li class="topline"><a href="#">text</a></li>'
              +'<li><a href="#">text</a></li></ul></li>'
              +'<li><a href="#" class="sub">text</a></li>'
              +'<li><a href="#" class="sub">text</a></li>'
              +'</ul></li><li><a href="#" class="menulink">text</a></li>'
              +'<li><a href="#" class="menulink">text</a>';
      
      }
      

      0 讨论(0)
    • 2021-02-04 15:17

      can I include HTML code in JS?

      If by that you mean can you output HTML via javascript then the answer is yes e.g.

      window.onload = function() {
          document.getElementById("menu").innerHTML = '<li class="topline"><a href="#">some text</a></li>  
                      <li><a href="#">some text </a></li>  
                      <li><a href="#">some text</a></li>  
                      <li><a href="#">some text</a></li>  
                      <li><a href="#"some text</a></li>  
                      <li><a href="#">some text</a></li>  
                      <li><a href="#">some text</a></li>';
      }
      
      0 讨论(0)
    • 2021-02-04 15:19
      document.getElementById("menu").innerHTML='
          <li class="topline"><a href="#">some text</a></li>
          <li><a href="#">some text </a></li>
          <li><a href="#">some text</a></li>
          <li><a href="#">some text</a></li>
          <li><a href="#"some text</a></li>
          <li><a href="#">some text</a></li>
          <li><a href="#">some text</a></li>
      ';
      

      All I really did was switch the quotes to single quotes so the HTML attributes don't mess the string up. If you did need to put single quotes in the innerHtml string, you can escape them with backslash, ie: innerHtml = ' Don\'t break me';

      0 讨论(0)
    • 2021-02-04 15:25

      A different way of doing it is to put the HTML inside a script tag:

      <script type="text/template" id="myHtml">
          <li class="topline"><a href="#">some text</a></li>
          <li><a href="#">some text </a></li>
          <li><a href="#">some text</a></li>
          <li><a href="#">some text</a></li>
          <li><a href="#"some text</a></li>
          <li><a href="#">some text</a></li>
          <li><a href="#">some text</a></li>
      </script>
      

      Then you can get it into Javascript using

      var myHtml = document.getElementById('myHtml').innerHTML;
      

      or using one of several libraries which help you with this. Code inside a script tag with type="text/template" will not be interpreted or shown by the browser. The advantage of this approach over putting it straight into a string in Javascript is that it allows you to keep treating it as normal HTML in your editor, and it keeps the Javascript clean. See also this post by John Resig.

      0 讨论(0)
    提交回复
    热议问题