1
我正在嘗試創建一個對話框,將對象參數中的每個鍵都作爲輸入。爲什麼在點擊時調用另一個函數?
function dialog(opt){
for (var key in opt) {
if (key=="msg" && typeof opt[key]==="string")
alert.append(opt[key]); //alert message
else if (typeof opt[key]==="function") { //alert functional button
console.log(key, "is a function");
$("<input type=button>").val(key).appendTo(alert)
.on("click",function(){ console.log(key, 'called'); opt[key](); close(); });
} else { //alert pretty button
console.log(key, "is not a function");
//$("<input type=button>").val(key).appendTo(alert).click(close);
}
}
}
dialog({msg:"Yes or no?",yes:function(){},no:''});
只有正在創建「是」輸入,但是當我點擊它,「不」被作爲函數調用:
yes is function dialog.js:41
no is not a function dialog.js:44
no called dialog.js:43
Uncaught TypeError: Property 'no' of object #<Object> is not a function
不,我認爲它會有所作爲,但你可以切出一些通過使用本地'typeof'比較運算符,jQuery的開銷。例如:'if(typeof opt [key] ===「function」)'。這可能是jQuery在內部做的。 – War10ck
好的我會盡我所能謝謝你,我一直在考慮改用javascript,但我非常習慣jQuery。 – shuji
我認爲這是一個關閉問題... –