我試圖在加載時使用自定義模板填充函數動態填充選擇標記(最新的jQM版本)。JQM - 僅在加載時注入動態內容
如果在「pagebeforechange」事件中調用了fn,則會正確初始化select標籤。由於每次頁面轉換都會調用此事件,因此我想將fn移至「pageinit」事件。這不起作用,大概是因爲DOM尚未完全可用。我如何強制jQM只在頁面中注入內容?目前,我正在使用一個kludge。肯定有一個更聰明的方法。感謝您的任何建議。
$(document).bind('pageinit', function() {
InitSelTagTest("#selActTag", "tplTag"); // Does not work.
});
$(document).bind("pagebeforechange", function (e, data) {
if ($("#selActTag").children().size() === 0) {
InitSelTagTest("#selActTag", "tplTag"); // Kludge, but it works
}
});
function InitSelTagTest(el,tpl) { // Append all tags to element el
var lstAllTags = JSON.parse($("#hidTag").val()); // Create tag array
// Retrieve html content from template.
var cbeg = "//<![" + "CDATA[", cend = "//]" + "]>";
var rslt = tmpl(tpl, { ddd: lstAllTags }).replace(cbeg, ").replace(cend,");
$(el).html(rslt).trigger("create"); // Add to DOM.
}
編輯 針對Shenaniganz」評論,似乎‘pagebeforecreate’事件可以做的伎倆,即。
$("#pgAct").live("pagebeforecreate", function() {
// Populate tag select. Works. Traversed only once.
InitSelTag("#selActTag", "tplTag");
});
也許不是你想找的,但你可以只設置一個標誌。 – Jack