參考文章:
HTML5 <template>標(biāo)簽元素簡(jiǎn)介
HTML語義化:HTML5新標(biāo)簽——template
template標(biāo)簽劲弦,顧名思義近范,模板的意思
HTML5提供的新標(biāo)簽勇吊,更加規(guī)范和語義化
可以把列表項(xiàng)放入template標(biāo)簽中辫愉,然后進(jìn)行批量渲染
注:template標(biāo)簽display屬性默認(rèn)為none
<template id="tpl">
<div class="linear-layout">
<span>靜音</span>
<i class="iconfont icon-duigou linear-icon"></i>
</div>
<div class="linear-interval"></div>
</template>
需要注意的是template標(biāo)簽的操作
直接使用原生JS操作署辉,
var template = document.getElementById('tpl');
console.log(template);
獲得到的DOM結(jié)構(gòu)是這樣的族铆,可以看到中間有一層#document-fragment
所以要操作template里面的dom,不能直接用template哭尝,要用template.content哥攘,如下
var template = document.getElementById('tpl');
var layout = template.content.querySelector('div');
var text = template.content.querySelector('span');