2010-07-09 94 views
1

can we programatically click on an a href tag using jquery? i have tried $('a#someid').click(); but this does not trigger anything.. although i am using $("a#someid").attr("href", "someurl") to set the href dynamically which works fine..是否有可能通過程序模擬點擊<a href> tag using jquery?

Also i tried giving window.location = "someurl", but when this is done.. the IE browsers security comes into play and asks for me to download file. which i need to avoid. if the same "someurl" is given as an href to an A tag the url is loaded without any IE securities..

Any help is very much appreciated..

+0

我從來沒有遇到過使用window.location之前的IE安全警告。你使用的是什麼版本的IE? – 2010-07-09 15:00:00

回答

3

Taken from : here

使用.trigger('click')不會觸發本機點擊事件。爲了模擬一個默認的點擊,你可以綁定一個單擊處理程序,像這樣:

$('#someLink').bind('click', function() { 
    window.location.href = this.href; 
    return false; 
}); 

...其中「someLink」是您的首選的實際選擇。

然後,您可以根據其他交互觸發該綁定點擊處理程序(如果需要)。

$('input.mycheckbox').click(function() { 
    if (this.checked) { 
    $('#someLink').trigger('click'); 
    } 
}); 
+0

thnx安東尼奧爲您的答覆.. 但事情是,如果我給任何URL到window.location.href該URL將被IE安全阻止。但如果相同的網址是給予A標籤的href,並手動點擊它。該網址將打開沒有任何IE安全阻止.. – vinay 2010-07-12 09:55:37

+0

你的一些大腿安全有:)對不起,沒有幫助。 – 2010-07-19 15:07:56

相關問題