hit*_*tzi 1 jquery element find root
我使用jQuery函数find()来提取html文件的div.我以这种方式使用它
data.find('#tpl_header')
Run Code Online (Sandbox Code Playgroud)
问题是jquery find()只找到非根元素.所以这不会起作用:
[...]
<body>
<div id="tpl_header" class="table header">
<div class="tr">
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
但这种方式有效:
[...]
<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>
Run Code Online (Sandbox Code Playgroud)
有没有办法找到这个模板div而不添加额外的不需要的div?
[加]
模板阅读功能 - 已经由Sjoerd在下面提到的更改:
function LoadTemplate()
{
$.get('templates/' + template + '/main.html',
function(data) {
data = $(data);
$('#header').html($('#tpl_header', data));
});
}
Run Code Online (Sandbox Code Playgroud)
var templateElement = $('#tpl_header')
Run Code Online (Sandbox Code Playgroud)
element.find()只查找该元素的后代,而$()在整个页面上查找元素.
| 归档时间: |
|
| 查看次数: |
2039 次 |
| 最近记录: |