在我的網頁上,我有一個類「編輯器」的DIV,我將其複製到變量中。如何訪問複製到變量的div的子項目
editorTemplate = $('.editor');
的DIV看起來像這樣(簡化):
<div class="editor">
<div>
Title: <span class="title" id="title"> the title goes here </span><br />
<select class="recording_list" id="recording_list">
<option value="1">Pos 1</option>
<option value="2">Pos 2</option>
...
</select>
</div> <!-- class=editor -->
後來我想將它添加到頁面創建從該div系列:
$(editArea).append(editorTemplate);
到目前爲止好。
但是,在將編輯器模板粘貼到頁面上之前,我想更改某些屬性 - 如字段的ID,某些文本和選項框的選定元素。
我可以改變編輯模板的ID與
$(myEdit).attr("id", "edit" + nEditors);
但我不知道如何訪問模板,例如內元素標題和「標題」字段的文本。
模板後粘貼到網頁,我可以說
$('#title').attr("id", "title" + nEditors);
$('#title').html("the new text");
...
是否有可能之前我貼模板到頁面中,使這些變化?
'在我的網頁我有一個類的DIV「主編「我將其複製到一個變量中。」你不是在複製這些元素。您只是創建一個名爲'editorTemplate'的jQuery包裝器變量,它是一個指向與選擇器匹配的DOM元素的指針。這一點很重要,以後在做'append(editorTemplate)'時,你會最終移動DOM元素而不是複製新的版本。如果您想製作DOM元素的副本,請稍後操作它們並添加它們,對您希望製作的元素使用[** clone()**](http://api.jquery.com/clone/)副本。 – Nope
非常感謝提示。 – PaulS