包括< script> < script type =" text / template">中的标记标签 [英] Including <script> tag in <script type="text/template"> tag
问题描述
我是一名新程序员,我有一个小问题。这是我的代码的一部分:
I am a new programmer and I have a little question. This is a part of my code:
<nav>
<ul>
<li><input type="button" value="HomePage" id="home1" data-page="home"></li>
<li><input type="button" value="About" id="about1" data-page="about"></li>
</ul>
</nav>
<div id="content">
<div class="page active">
<div class=firstLoad>
(some content...)
</div>
</div>
</div>
<script type="text/template" id="home">
<div id="backImage">
<div id="spinner">
</div>
<div>
<input type="button" value="..." id="car" class="button">
</div>
<div>
<input type="button" value="..." id="Car1" onclick="location.href='#'" class="button">
</div>
<div>
<input type="button" value="..." id="reset" class="button">
</div>
</div>
<script src="location.js"></script>
</script>
我的问题是如何在html页面中使用这个location.js脚本标记(id =home)当我用innerHTML调用这个html页面时,我改变了当前出现的页面?
无论我做了什么,这个脚本在html页面中根本不适用。
My question is how can I use this "location.js" script tag in the html page (id="home") when I call this html page with innerHTML and I change the current appear page? No matter what I have done this script doesn't work for me at all in the html page.
谢谢......)
推荐答案
不,这不适用于您。在另一个< script>
内部有一个< script>< / script>
即使浏览器能够正确解析,它也不起作用。但是,您只需提供您想要以另一种方式加载的JavaScript文件,例如在数据集
属性中。另外,正如Quentin所指出的那样, text / template
不是标准类型,所以前缀为 x -
No, that's not going to work for you. Not only is it invalid to have a <script></script>
inside of another <script>
, it wouldn't work even if the browser could parse it correctly. However, all you need to do is provide the javascript files you want to load in another manner, like in a dataset
attribute. Additionally, as Quentin pointed out, text/template
isn't a standard type, so prefix it with x-
as standard practice
<script type="text/x-template" id="home" data-jsassets="location.js,/path/to/some/other.js">
...
</script>
现在,检索JavaScript的逗号分隔列表以加载并注入页面,运行脚本:
Now, retrieve the comma-delimted list of javascript to load and inject it into the page, which will run the script:
var tpl, jsassets, tag, i,l;
tpl = document.getElementById('home');
// At this point, ensure your template has been rendered and attached to the page
// by your template processor
jsassets = (tpl.getAttribute('data-jsassets') || '').split(',');
for(i = 0, l = jsassets.length; i < l; i++){
tag = document.createElement('script');
tag.type = "text/javascript";
tag.src = jsassets[i];
document.head.appendChild(tag);
}
这篇关于包括< script> < script type =" text / template">中的标记标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!