2010-07-09 58 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

相关问题