html模板标记和jquery
我遇到了这样的情况,我试图在html源代码中使用html模板标记和jquery,jquery,html,html5-template,templatetag,Jquery,Html,Html5 Template,Templatetag,我遇到了这样的情况,我试图在html源代码中使用标记: <template id="some-id"> <div id="content-container"> <span>{{some_string}}</span> <div> </template> 我把这个拿回来: <template id="some-id"> #document-fragment <div id=
标记:
<template id="some-id">
<div id="content-container">
<span>{{some_string}}</span>
<div>
</template>
我把这个拿回来:
<template id="some-id">
#document-fragment
<div id="content-container">
<span>{{some_string}}</span>
<div>
</template>
这将导致以下错误:
Error: Syntax error, unrecognized expression: <the html string>
错误:语法错误,无法识别的表达式:
我不知道这个错误是不是由mustache语法引起的。我想一定有一个jQuery解决方案可以解决这种行为,但当我用Google搜索时,我会得到大量jQuery模板的点击,这是不同的
是否有人有想法、答案或指向网站/页面的指针可以帮助我解决这个问题?我在努力避免拼凑一些粗俗的东西
编辑:我最终找到了这个解决方案(我仍在测试它,以确保它是一个解决方案,但它看起来很有希望)
template=$(“#某些id”)
content=template.html()
新分区=$(“”)
新建_div.html(内容)
我最终得到了那个我以前并不真正需要的包含div的文件,但我可以接受它。但是这种感觉很糟糕。有人有更好的方法吗?好处是,它仍然可以在尚未完全适应模板标记行为的浏览器中工作
谢谢 试试看:
var myTemplate = $("#some-id").html().trim();
var myTemplateClone = $(myTemplate);
我相信这个网站将有助于解释ShadowDOM是如何工作的,以及模板是如何与之交互的。 此外,克隆影子模板的节点实际上非常简单,甚至不需要使用jquery 下面是一个jfiddle演示: HTML:
$(“#某些id”).html
应该是$(“#某些id”).html()。我会解决它的。看看jquery。克隆它听起来好像对你有帮助^^我试过了,但实际上它将内容克隆为模板。所以我需要的东西仍然只是一个文档片段,在概念DOM之外。我可能已经找到了答案,但我不确定这有多困难,或者是否有更好、更友善的方法。我将把它添加到我的问题中,希望有人能告诉我它是坏的还是好的。@deadManN不知道你从哪里学到的;根据1999年发布的声明:“ID和名称标记必须以字母([a-Za-z])开头,后面可以跟任意数量的字母、数字([0-9])、连字符(“-”)、下划线(“)、冒号(:”)和句点(.”)。他们甚至在示例中使用它。HTML 5甚至删除了这些限制,并且id
“必须至少包含一个字符[且]不得包含任何空格字符”。请小心使用“content”标记,它已被HTML5规范拒绝:。这是对content标记的不同使用,与此处解释的不同。content标签用于将信息从shadow根外部投影到shadow根内部的数据谢谢您的帮助-修剪-我需要的!不需要trim()
。与@dtracers的纯javascript解决方案相比,这个jQuery解决方案(复制字符串)似乎失去了性能,这很有趣。
Error: Syntax error, unrecognized expression: <the html string>
template = $("#some-id")
content = template.html()
new_div = $("<div></div>")
new_div.html(content)
var myTemplate = $("#some-id").html().trim();
var myTemplateClone = $(myTemplate);
<div id = "hoster">
<span class = "title">Title</span>
<span class = "id">51ab89af</span>
</div>
<template id = "im-a-template">
<h1><content select =".title"></content></h1>
<h1>Class Id: <content select = ".id"></content></h1>
<input type="text" placeholder = "Name"></input>
</template>
// find where you want to put your shadow dom in
var host = document.querySelector('#hoster');
var root = host.createShadowRoot(); // create the host root
var template = document.querySelector('#im-a-template');
// this is the node of the object you wanted
var documentFragment = template.content;
// this is a cloned version of the object that you can use anywhere.
var templateClone = documentFragment.cloneNode(true);
root.appendChild(templateClone); // this empty root now has your template