feixiablog

把想要的结构写在HTML里,用一个display:none的标签来包裹。

一般而言,会使用:

  • 不设置为type="text/javascript"的script标签
  • textarea标签
<script type="text/html" id="theTemplate">
    编写你的html结构
</script>


<textarea id="theTemplate" style="display:none">
编写你的html结构
</textarea>
使用这些标签,是为了避免这些不被直接使用的标签一被浏览器读取到就被渲染。而且这样做的话,里面的img的src也不会被浏览器自动获取。

如果你使用script标签,就可以如下代码得到它:
//使用script包裹
var template = document.getElementById("theTemplate").innerHTML ;
//使用textarea包裹
var template = document.getElementById("theTemplate").value ;

使用时textarea时要注意,textarea无法嵌套自身;script标签也无法自嵌套

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-12-03
  • 2022-12-23
  • 2021-04-20
  • 2022-12-23
  • 2021-10-06
  • 2021-12-13
猜你喜欢
  • 2021-12-09
  • 2021-12-19
  • 2022-12-23
  • 2021-08-31
  • 2022-01-07
  • 2021-06-03
  • 2022-12-23
相关资源
相似解决方案