我試圖創建一個Web應用程序,將允許用戶定義自定義JavaScript函數,然後添加一個按鈕,他們的用戶界面,以及預製件的功能。jQuery的綁定自定義函數附加元素
下面是代碼
var customCommands = {
command1: {
text: 'Hello Console',
cFunctionRun: function() {
console.log('hello Console!');
}
},
command2: {
text: 'Hello World',
cFunctionRun: function() {
alert('hello World!');
}
}
}
然後我寫了一個小功能循環,雖然並建立按鈕,並將它們添加到用戶界面的一個樣本。問題是,當我的元素追加到比點擊按鈕沒有工作時用戶界面...
這裏是我試過
for (var cmd in customCommands) {
command = customCommands[cmd];
button = $('<button/>').html(command.text).on('click',
function(){
console.log(command.text);
command.cFunctionRun();
}
);
}
buttonContainer.append(button);
的方法之一現在我的循環建立的一切只是罰款,甚至.on('click')
的作品,但它總是顯示的歷史添加命令的文本?
這裏是http://jsfiddle.net/nbnEg/顯示發生了什麼。
你能加入更多內容?什麼你已經展示作品http://jsfiddle.net/nbnEg/ – bfavaretto
第二代碼在看...生病立即更新,包括循環 –