我有這樣的代碼。當我在一個函數中使用硬編碼的jquery對象時,一切正常。但是當我想將它傳遞給一個函數調用時,我的函數不能識別jQuery對象,並且表沒有繪製。無法將jQuery對象傳遞給函數。如何安全地將jquery對象傳遞給函數?
// This is a function that draws a table.
// I pass it the following params:
drawTbl({
tbody: $("#tbl tbody"), // <tbody> element, jq object, This doesn't work.
tblElem: null,
tblTmpl: null,
tblContTmpl: "cont_tmpl", // id of a jQuery template
justAll: res.justAll, // some data for a table
});
// This is a function declaration
// It doesn't draw a table if I pass tbody as a jquery object.
// But works if I hard code tbody
drawTbl = function(drawTblParams) {
drawTblParams.tbody.empty();
// Loop to draw a table with jquery template
for (var m in drawTblParams.justAll) {
// This doesn't work, content isn't appended to tbody
$.tmpl(drawTblParams.tblContTmpl, { i: drawTblParams.justAll[m] }).appendTo(drawTblParams.tbody);
// This works fine, content is appended to tbody
$.tmpl(drawTblParams.tblContTmpl, { i: drawTblParams.justAll[m] }).appendTo($("#tbl tbody"));
}
// The most ridiculous thing
// This returns false! But it has to be the same element!
console.log(drawTblParams.tbody == $("#tbl tbody"));
};
爲什麼jq對象失去其價值?如何將jquery對象傳遞給函數?