2012-10-15 55 views
0

For some reason, I cannot use jquery.模擬鏈路(<a href='dd'>) click with javascript

With plain javascript, how could I simulate the link-click action on a web page.

Thanks.

Below is the javascript I tried to get online, but it seems not work.

function create_window (title, content){ 

    var comment = document.getElementById('modal'); 
alert(comment.innerHTML); 
    if (comment.click) { 
     alert('hi'); 
     comment.click(); 
    } else if (document.createEvent) { 
     alert('3'); 
     var ev = document.createEvent('MouseEvents'); 
     ev.initMouseEvent('click', true, true, window, 1, 12, 345, 7, 220, false, false, true, false, 0, null) 
     comment.dispatchEvent(ev); 
    } 

} 

回答

0
function simulateClick() { 
    var evt = document.createEvent("MouseEvents"); 
    evt.initMouseEvent("click", true, true, window, 
    0, 0, 0, 0, 0, false, false, false, false, 0, null); 
    var cb = document.getElementById("link"); 
    var canceled = !cb.dispatchEvent(evt); 
} 

from hereHere是小提琴,我在其中定義的函數來創建和調度click事件,1秒的延遲後添加事件處理程序的元素,並調用該函數。

+0

抱歉,但是這一點,我只是得到一個警報彈出「取消」 ... –

+0

我會希望用戶點擊被點擊與模擬... –

+0

嗨Asad,與小提琴,你認爲我需要嵌入像jquery或motools一些圖書館使它工作? –

0

嘗試這

<a href="doc.pdf"><span id="spnDownload">Download!</span></a> 

$(「#spnDownload」)點擊();

+0

這對任何人都適用嗎? - 對我來說,它不是。 – badera