您可以使用.one()
有一個事件處理程序,只觸發一次:
$(function() {
//bind a mouseover event handler that will fire only once
$("#page-flip").one('mouseover', function() {
//set a timeout so the code runs after half a second
setTimeout(function() {
//run your code here
$(".box").toggleClass("box-change");
$(".films").toggleClass("films-change");
$(".synopsis").toggleClass("synopsis-change");
}, 500);
});
});
這裏是一個演示:http://jsfiddle.net/jasper/fWakf/3/
文檔:http://api.jquery.com/one
您也可以使用.off()
:
$(function() {
$("#page-flip").on('mouseover', function() {
//remove this event handler so it doesn't fire in the future
$(this).off('mouseover');
setTimeout(function() {
$(".box").toggleClass("box-change");
$(".films").toggleClass("films-change");
$(".synopsis").toggleClass("synopsis-change");
}, 500);
});
});
這裏是一個演示:http://jsfiddle.net/jasper/fWakf/4/
注意.on()
jQuery的1.7,在這種情況下,新的情況是相同的.bind()
。 .off()
也是新的,所以如果您使用的版本大於1.7和.bind()
,請使用.unbind()
。
你知道嗎''unbind''? http://api.jquery.com/unbind/ – ckruse 2012-02-28 20:48:04