[英]jQuery auto click on a link
Ok, so I searched all over with no answer. 好的,所以我搜遍了所有没有答案。 Can someone explain why
有人可以解释原因
does not work with .trigger('click')
不适用于
.trigger('click')
<a id="openNew" href="http://www.example.org">Click me</a>
<script type='text/javascript'>
$(window).load(function(){
$('#openNew').addClass("external").attr({ target: "_blank", href: 'http://www.google.com' }).trigger('click');
});
</script>
and it does not work with .click()
并且它不适用于
.click()
<script type='text/javascript'>
$(window).load(function(){
$('#openNew').addClass("external").attr({ target: "_blank", href: 'http://www.google.com' }).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.
在这上工作大约1个小时,让我发疯,我知道我一定是在做一些愚蠢的事情。
JsFiddle for your convenience. JsFiddle为您提供方便。
I wouldn't mind any other solution in plain JavaScript. 在简单的JavaScript中,我不介意任何其他解决方案。
Use elem[0].click();
使用
elem[0].click();
instead of elem.click();
而不是
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). 顺便说一句:弹出窗口拦截器会阻止它实际打开一个新窗口(幸运的是)。
Simulating a user physically clicking the link is not possible. 无法模拟用户物理点击链接。 Since you are using target='_blank' I presume you want a new window?
既然你使用的是target ='_ blank',我认为你想要一个新窗口? So you'll need to use window.open.
所以你需要使用window.open。 Which popup blockers wont like.
哪个弹出窗口拦截器不喜欢。
Actually it's clicked, but not opened link.. check out here http://jsfiddle.net/H2KuF/5/ 实际上它被点击了,但没有打开链接..请看这里http://jsfiddle.net/H2KuF/5/
Probably you need to open new browser window with this link from JS. 可能你需要用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 comments) Karl swedberg 在此声明 (其中一条评论)
Using .trigger('click') will not trigger the native click event.
使用.trigger('click')不会触发本机点击事件。
Doing the following will work: 执行以下操作将起作用:
<a id="openNew" href="http://www.example.org">Click me</a>
<script type='text/javascript'>
$(window).load(function(){
$('#openNew').addClass("external").attr({ target: "_blank", href: 'http://www.google.com' })[0].click();
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.