在HTML模板中选择一个元素

时间:2014-08-07 19:34:12

标签: javascript jquery templates find placeholder

我有一个模板如下:

<script type="text/template" id="editorItemPlacholder">
    <div>
    ...
    </div>
</script>

并创建一个模板实例,如下所示:

var editorItemTemplate = $("#editorItemPlacholder").html();

然后我想在每次创建它的实例时更改模板内元素的id。所以我想我必须使用它:

editorItemTemplate.find("div").prop("id",item);

但这不起作用。并且标志是代码不在此行之后。这种选择有什么问题?如何在该模板中进行更改。

1 个答案:

答案 0 :(得分:0)

您在html元素中使用jQuery函数。您无法将find用于$editorItemTemplate,其中包含模板的html。

您必须使用以下方式找到元素:

$('div', $editorItemTemplate).prop('id', item);

问候。