html语法:json与javascript对象

时间:2016-07-22 15:17:50

标签: javascript html json

存储html内容时,json和js是否需要不同的html语法?

例如此处:以下js对象中的'+。我的数据是在json数组中,但似乎略有不同。

我只熟悉在HTML文档中使用html数据,并且很难找到关于这些基本内容的信息。



var contentString = '<div id="content">'+
      '<div id="siteNotice">'+
      '</div>'+
      '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
      '<div id="bodyContent">'+
      '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
      'sandstone rock formation in the southern part of the '+
      'Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi) '+
      'south west of the nearest large town, Alice Springs; 450&#160;km '+
      '(280&#160;mi) by road. Kata Tjuta and Uluru are the two major '+
      'features of the Uluru - Kata Tjuta National Park.</p>'
      '</div>'+
      '</div>';
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:-1)

对于最佳做法,您可以从DOM中存在的template标记中获取HTML代码,例如:

var contentString = $('#mydiv').html(); //with jquery

var contentString = document.getElementById('mydiv').innerHTML; //with vanilla javascript

template应该有你的infowindow模板,如下所示:

<template id="myDiv">
  <div id="content">
    <!-- your infowindow html code goes here -->
  </div>
</template>

为什么template代码?因为它会为用户隐藏。如果您愿意,可以使用div标签或任何隐藏样式的标签。