我正在研究這個菜單系統,它與操作系統如何操作非常相似。 使用jquery等Javascript怪異 - 我無能爲力
我有2條評論在For循環。它基本上輸出$(document).on('click')...函數中的最後一個索引。但在document.on之外,它工作正常。
這可能只是一個明顯的問題,但我已經花了大約一個小時在此..在此先感謝!
menu: function(title) {
this.title = title;
this.slug = slugify(title);
this.icon = false;
this.buttons = Object();
this.num_buttons = 0;
this.visible = false;
this.timeout_id = null;
this.is_hovering_dropdown = false;
this.is_hovering_menu = false;
this.render = function() {
var that = this;
var slug = that.slug;
var str = '<li id="menu-' +slug +'"><a href="#" id="menu-toggle-' +slug + '">' + this.title + '</a>';
if (this.num_buttons > 0) {
str += '<ul id="menu-dropdown-' + slug + '" style="display: none;" class="dropdown">';
for (var button in this.buttons) {
str += '<li><a href="#" id="menu-dropdown-' + slug + '-' +that.buttons[button]['slug'] +'">' +that.buttons[button]['title'] +'</a></li>'
alert(button) //new project, open project, save as etc.
$(document).on("click", "#menu-dropdown-" +slug + '-' + that.buttons[button]['slug'], function() {
$("#menu-dropdown-" + slug).hide("fade", 200);
that.visible = false;
alert(button);//save as, save as, save as, save as etc.
});
}
}
}
}
你有snipet你的html嗎? – webdad3 2012-07-25 21:41:00
您是否在調用menu.render之前等待DOM加載? – Kasapo 2012-07-25 21:42:28
在JavaScript中ew html concat。你應該使用像JsRender這樣的模板引擎,並保持你的理智! – 2012-07-25 21:43:14