我想使用一個變量來存儲我要綁定的事件。jQuery綁定到可變事件
喜歡的東西:
var the_event = 'mouseover';
$().bind(the_event, function(){
console.log('hi there!');
});
我能做到同樣的事情if
S,但我寧願用一種更好的方式,一個可能存在的!
我想做的事,與if
叫什麼:
var the_event = 'mouseover';
var the_function = function(){
console.log('hi there!');
}
if(the_event == 'mouseover'){
$().bind('mouseover', the_function);
} else if(the_event == 'click'){
$().bind('click', the_function);
}
謝謝!
編輯:
什麼,我其實特林做的是:
var events = {
one: 'mouseover',
two: 'mouseout'
};
$().bind({
events.one: function(){
console.log('1');
},
events.two: function(){
console.log('2');
},
);
你試過你的第一個例子嗎? –
你是第一個例子是完全正確的。你已經回答你是自己的問題。我強烈建議實際上將選擇器放入jQuery的$()調用中。 – DefyGravity
當你說「我可以做到同樣的事情」時,你在說什麼?跟什麼一樣?你的第一個例子應該工作得很好。 – rossipedia