how to assign a block of html code to a javascript variable

后端 未结 7 1381
天涯浪人
天涯浪人 2020-12-04 18:02

what is the syntax to store a block of html code to a javascript variable?

test.test
相关标签:
7条回答
  • 2020-12-04 18:51

    you can make a javascript object with key being name of the html snippet, and value being an array of html strings, that are joined together.

    var html = {
      top_crimes_template:
        [
          '<div class="top_crimes"><h3>Top Crimes</h3></div>',
          '<table class="crimes-table table table-responsive table-bordered">',
            '<tr>',
              '<th>',
                '<span class="list-heading">Crime:</span>',
              '</th>',
              '<th>',
                '<span id="last_crime_span"># Arrests</span>',
              '</th>',
            '</tr>',
          '</table>'
        ].join(""),
      top_teams_template:
        [
          '<div class="top_teams"><h3>Top Teams</h3></div>',
          '<table class="teams-table table table-responsive table-bordered">',
            '<tr>',
              '<th>',
                '<span class="list-heading">Team:</span>',
              '</th>',
              '<th>',
                '<span id="last_team_span"># Arrests</span>',
              '</th>',
            '</tr>',
          '</table>'
        ].join(""),
      top_players_template:
        [
          '<div class="top_players"><h3>Top Players</h3></div>',
          '<table class="players-table table table-responsive table-bordered">',
            '<tr>',
              '<th>',
                '<span class="list-heading">Players:</span>',
              '</th>',
              '<th>',
                '<span id="last_player_span"># Arrests</span>',
              '</th>',
            '</tr>',
          '</table>'
        ].join("")
    };
    
    0 讨论(0)
提交回复
热议问题