是否有可能宣佈在var
jquery.live()
(以及.delegate()
& .on()
),它可以在所有的函數中使用?申報VAR(),它可以在所有功能
例子:
$("#s4-leftpanel-content ul.root > li.static > a").live({
click: function (event) {
var hasChildren = $(this).parent('li').children('ul').length > 0;
if (hasChildren) {
event.preventDefault();
$("#s4-leftpanel-content ul.root li.static > ul.static").hide();
$(this).parent('li').children('ul').toggle();
}
},
mouseover: function() {
$(this).css('background-color', 'green');
},
mouseout: function() {
$(this).css('background-color', '');
}
});
可以說,我想用布爾VAR hasChildren
在mouseover
和mouseout
功能以及,我則必須聲明,var
又在這兩個函數或者是有辦法我可以在這個當前對象中全局地聲明它?
雖然這在技術上是正確的,但我認爲這不合理,因爲''click''之前'mouseover'被觸發*。據我瞭解,OP希望在所有處理程序中執行'hasChildren'代碼而不復制它。 – pimvdb 2012-01-02 10:29:32
@pimvdb:哦,現在我明白了......謝謝! – 2012-01-02 10:32:07