" type="hidden"/>

js模版使用


<div></div>

<script type="text/html" id="javascript_template">   <div onclick="_dom()">   <ul id="wrap">    <li>1</li>   <li>2</li>   </ul>   </div> </script>

1.首先这种写,在页面渲染的时候,浏览器不会读取script标签中的html代码

2.外面不能获取到里面的div节点

so: 

       在使用时,要在script标签上加个ID可以供我们找到它,

       即然做模版用,且里面的html代码不会被渲染读取,则需在外面再新建一个div来当容器,装下这些html代码,

       即我们可以通过 

var _html=document.getElementById('javascript_template').innerHTML;
document.getElementsByTagName('div')[0].innerHTML=_html;

然后我们模版里的htmll代码就可以运行在页面中了;

 

如果遇到html代码加入外部div后,获取不到节点的情况,可以在外部写个函数,然后在html内部用行内调用 onclick="_dom()"

function _dom() {
  document.getElementById('wrap')
}

原文链接:https://www.cnblogs.com/MrZouJian/p/5614581.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



猜您在找 html template & script type="text/html" script标签中type为"text/x-template"或"text/html" Refused to execute script from '....js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.md 是干什么的,为什么要把html写在js中? 这是什么编程语言风格,都能这样用吗?">