2013-06-12 43 views
0

美好的一天。爲什麼觸發點擊不起作用?

我有代碼:

<a class="fancybox iframe" href="http://google.com" id="online_form_a">test</a> 

<script> 
$(document).ready(function() { 
    // add the fancy box click handler here 
    setTimeout(function() { 
     $("#online_form_a").trigger('click'); 
    },10); 
}); 
</script> 

此代碼應在10秒後點擊元素<a>,但腳本無法正常工作。

請告訴我,哪裏錯誤?

P.S:看做工腳本可以在JsFiddle

+0

觸發點擊不會重定向您。你必須改變'window.location'你的自我。 – elclanrs

+0

@elclanrs你不對。觸發器應該點擊'' –

+0

我不會進行清理。我只需要點擊一下,開始fancebox –

回答

0

在這裏,我想你想自動重定向到a的href的值,也不會發生使用.trigger()

觸發方法將觸發註冊的事件處理程序,但可能不會觸發與默認操作關聯的元素。

從文件

雖然.trigger()模擬的事件激活,完成一個 合成事件對象,它不完美地複製一個 天然存在的事件。

另一種選擇是.triggerHandler(),但即使這不會導致默認行爲

的.triggerHandler()方法不會導致 事件的默認行爲發生(諸如表單提交)。

所以解決的辦法是window.location這裏使用

window.location = $('#online_form_a').attr('href') 
0
<a class="fancybox iframe" href="http://google.com" id="online_form_a" >test</a> 
$(文件)。就緒(函數(){ //添加花哨中點擊這裏處理 的setTimeout (function(){ location.href = $(「#online_form_a」)。attr('href'); },1000); });
3

觸發點擊事件意味着你調用綁定到element..without的單擊事件功能點擊元素

但有連接到您的元素沒有點擊事件..

做到像這...

<a class="fancybox iframe" href="http://google.com" id="online_form_a">test</a> 


$(document).ready(function() { 
    // add the fancy box click handler here 

    $('a').click(function(){ 
     window.location.href = "http://google.com"; 
    }); 

    setTimeout(function() { 
     $("#online_form_a").trigger('click'); 
    },10000);    // this value is in milliseconds(1 sec = 1000 ms) 
}); 
相關問題