我使用jQuery函数find()来提取html文件的div。我以这种方式使用它
data.find('#tpl_header')
问题是jquery find()只找到非根元素。 所以这不会起作用:
[...]
<body>
<div id="tpl_header" class="table header">
<div class="tr">
</div>
</div>
</body>
</html>
但这种方式有效:
[...]
<body>
<div id="template"> <!-- because jQuery find function did not find root elements! -->
<div id="tpl_header" class="table header">
<div class="tr">
</div>
</div>
</div>
</body>
</html>
有没有办法找到这个模板div而不添加额外的不需要的div?
[ADD]
模板阅读功能 - 已经在下面由Sjoerd提及:
function LoadTemplate()
{
$.get('templates/' + template + '/main.html',
function(data) {
data = $(data);
$('#header').html($('#tpl_header', data));
});
}
答案 0 :(得分:5)
var templateElement = $('#tpl_header')
element.find()
只找到该元素的后代,而$()
在整个页面上找到元素。
答案 1 :(得分:0)
另一个主题为我提供了一个有效的解决方案我必须使用.filter()函数来获取根div。