这是不可能的;浏览器不会将<script>
标记中的HTML内容视为DOM的一部分。当您使用$('#idhere').html()
检索<script>
标记的内容时,您会收到字符串结果。
要回答特洛伊的问题,他是最有可能包括在他的文件的<head>
模板,以便他能够最终动态地呈现在浏览器端的内容。但是,如果是这种情况,则OP应使用与text/html
不同的MIME类型。您应该使用未知的MIME类型,例如text/templates
- 使用text/html
会混淆内容的用途。
我猜测你想达到入<script>
标签和抢div
是因为你的单<script>
标签中内置较小的子模板的原因。那些较小的模板应该放置在它们自己的<script></script>
标记中,而不是包含在一个大的<script></script>
标记对中。
因此,而不是:
<script type="text/template" id="big_template">
<div id="sub_template_1">
<span>hello world 1!</span>
</div>
<div id="sub_template_2">
<span>hello world 2!</span>
</div>
</script>
这样做:
<script type="text/template" id="template_1">
<span>hello world 1!</span>
</script>
<script type="text/template" id="template_2">
<span>hello world 2!</span>
</script>
您的标记无效。 – undefined 2012-08-16 22:55:20
你想用模板做些什么吗? http://ejohn.org/blog/javascript-micro-templating/ – dana 2012-08-16 22:56:30
@undefined:当使用像'knockoutJs'或'Handlebars'这样的库时,例如你必须定义模板。 – Nope 2012-08-16 23:17:05