admin管理员组文章数量:1392007
Ok, so I searched all over with no answer. Can someone explain why
does not work with .trigger('click')
<a id="openNew" href="">Click me</a>
<script type='text/javascript'>
$(window).load(function(){
$('#openNew').addClass("external").attr({ target: "_blank", href: '' }).trigger('click');
});
</script>
and it does not work with .click()
<script type='text/javascript'>
$(window).load(function(){
$('#openNew').addClass("external").attr({ target: "_blank", href: '' }).click();
});
</script>
Does not click the link whatever I do. It only works if I click it. How can I make it auto click? Working on this for about 1 hour and is driving me crazy, I know I'm must be doing something stupid.
JsFiddle for your convenience.
I wouldn't mind any other solution in plain JavaScript.
Ok, so I searched all over with no answer. Can someone explain why
does not work with .trigger('click')
<a id="openNew" href="http://www.example">Click me</a>
<script type='text/javascript'>
$(window).load(function(){
$('#openNew').addClass("external").attr({ target: "_blank", href: 'http://www.google.' }).trigger('click');
});
</script>
and it does not work with .click()
<script type='text/javascript'>
$(window).load(function(){
$('#openNew').addClass("external").attr({ target: "_blank", href: 'http://www.google.' }).click();
});
</script>
Does not click the link whatever I do. It only works if I click it. How can I make it auto click? Working on this for about 1 hour and is driving me crazy, I know I'm must be doing something stupid.
JsFiddle for your convenience.
I wouldn't mind any other solution in plain JavaScript.
Share Improve this question edited Oct 15, 2015 at 17:30 Captain Obvlious 20.1k5 gold badges48 silver badges80 bronze badges asked Sep 1, 2011 at 7:11 Mihai IorgaMihai Iorga 39.7k17 gold badges108 silver badges109 bronze badges 2- This is a duplicate question. You're asking how to simulate a click of an anchor tag using jquery click/trigger. Here is a previous post with your answer. – kasdega Commented Sep 1, 2011 at 7:32
- JsFiddle Showing that the click even is being triggered. – kasdega Commented Sep 1, 2011 at 7:35
4 Answers
Reset to default 1Simulating a user physically clicking the link is not possible. Since you are using target='_blank' I presume you want a new window? So you'll need to use window.open. Which popup blockers wont like.
Use elem[0].click();
instead of elem.click();
since you want to call the native click function and not just trigger the click event.
By the way: Popup blockers will prevent this from actually opening a new window (luckily).
Actually it's clicked, but not opened link.. check out here http://jsfiddle/H2KuF/5/
Probably you need to open new browser window with this link from JS.
here is samples I found:
function open2(url, opt){
if (opt == 0) // current window
window.location = url;
else if (opt == 1) // new window
window.open(url);
else if (opt == 2) // background window
{window.open(url); self.focus();}
}
Karl swedberg states here (One of the ments)
Using .trigger('click') will not trigger the native click event.
Doing the following will work:
<a id="openNew" href="http://www.example">Click me</a>
<script type='text/javascript'>
$(window).load(function(){
$('#openNew').addClass("external").attr({ target: "_blank", href: 'http://www.google.' })[0].click();
});
</script>
Demo here
本文标签: javascriptjQuery auto click on a linkStack Overflow
版权声明:本文标题:javascript - jQuery auto click on a link - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744764463a2623952.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论