setTimeout內(nèi)不支持jquery的選擇器的解決方案
來源:易賢網(wǎng) 閱讀:1316 次 日期:2015-04-29 14:49:47
溫馨提示:易賢網(wǎng)小編為您整理了“setTimeout內(nèi)不支持jquery的選擇器的解決方案”,方便廣大網(wǎng)友查閱!

今天在寫一個(gè)js延時(shí)事件的時(shí)候發(fā)現(xiàn)在setTimeout這個(gè)方法里面使用jquery的$(this)竟然不起作用,各種測(cè)試,最后得出結(jié)論是setTimeout內(nèi)不支持jquery的選擇器。于是請(qǐng)教了一下QQ上做jquery開發(fā)的高手,馬上就把這問題解決了,在這里記錄一下。

下面是作者做延時(shí)處理時(shí)的js代碼:

$('.dl_select dt').hover( function(){ clearTimeout(t3); $(this).siblings('dd').css({'display':'block','cursor':'pointer'}); }, function(){ t2=setTimeout(function(){$(this).siblings('dd').css({'display':'none'});},300); } ); $('.dl_select dd').hover( function(){ clearTimeout(t2); $(this).css({'display':'block','cursor':'pointer'}); }, function(){ t3=setTimeout(function(){$(this).css({'display':'none'});},200); } );

注意以上代碼中得setTimeout里的代碼,如果這些代碼不在這個(gè)方法里面,本身是沒有問題的,但是如上這種情況是會(huì)報(bào)錯(cuò)的。至于原因,作者現(xiàn)在也沒有弄明白。經(jīng)網(wǎng)友點(diǎn)化改成如下就沒事兒了,方法很巧妙。如下是正確的代碼:

$('.dl_select dt').hover( function(){ clearTimeout(t3); $(this).siblings('dd').css({'display':'block','cursor':'pointer'}); }, function(){ var $this=$(this).siblings('dd'); t2=setTimeout(function(){$this.css({'display':'none'});},300); } ); $('.dl_select dd').hover( function(){ clearTimeout(t2); $(this).css({'display':'block','cursor':'pointer'}); }, function(){ var $this=$(this); t3=setTimeout(function(){$this.css({'display':'none'});},200); } );

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看腳本欄目
易賢網(wǎng)手機(jī)網(wǎng)站地址:setTimeout內(nèi)不支持jquery的選擇器的解決方案
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn)

版權(quán)所有:易賢網(wǎng)