Javascript 我可以触发点击事件 onload
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4966832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
can I trigger click event onload
提问by Gowri
I am having anchor tag in my page. I like to trigger click event onload . Which means I wanna open this page "http://XXXXX.com" with new tab. Because I don't wanna popup blockers. Is there anyway to do this?
我的页面中有锚标记。我喜欢触发点击事件 onload 。这意味着我想用新标签打开这个页面“http://XXXXX.com”。因为我不想弹出窗口拦截器。有没有办法做到这一点?
anchor attrs are given bellow
锚属性如下
id="add_redirect"
href="http://XXXXX.com"
target="_blank"
采纳答案by T.J. Crowder
If your goal is to bypass pop-up blockers on page load, triggering the click
event synthetically probably won't work. Browsers are smart enough to know when a click
is user-generated vs. when you've called the click
function on the DOM element (on those browsers were that even works). Examples: http://jsbin.com/avibi3/3, http://jsbin.com/avibi3/4
如果您的目标是在页面加载时绕过弹出窗口阻止程序,则click
合成触发事件可能不起作用。浏览器足够聪明,可以知道什么时候 aclick
是用户生成的,什么时候你click
在 DOM 元素上调用了这个函数(在那些浏览器上甚至可以工作)。示例:http://jsbin.com/avibi3/3、http://jsbin.com/avibi3/4
Using jQuery's trigger
mechanism certainly won't do it, because it doesn't really trigger a click
event at all; it just fires the handlers that jQuery hooked up (edit: and, apparently, ones defined via an onclick
attribute — see Sukhi's answer — but not ones attached via addEventListener
). If that's what you want to do, Sukhi's answer shows you how, although I always say: If you want code to be run from two different places, put it in a function, and call that function from two different places (rather than putting it in a click
handler and then simulating a click just to run the code). There are valid use cases for trigger
(mostly relating to integrating with third-party scripts), but for running your owncode from two different places, it's a symptom of a design problem.
使用 jQuery 的trigger
机制肯定不行,因为它根本不会真正触发click
事件;它只是触发 jQuery 连接的处理程序(编辑:显然,通过onclick
属性定义addEventListener
的处理程序- 请参阅 Sukhi 的回答 - 但不是通过 附加的处理程序)。如果这就是你想要做的,Sukhi 的回答会告诉你如何做,尽管我总是说:如果你想让代码从两个不同的地方运行,把它放在一个函数中,然后从两个不同的地方调用这个函数(而不是把它放在在click
处理程序中,然后模拟单击以运行代码)。有有效的用例trigger
(主要与与第三方脚本集成有关),但用于运行您自己的来自两个不同地方的代码,这是设计问题的征兆。
回答by Shwet
Yeah, you can use a click event onLoad()
. Just use the setTimeout()
method in jquery. That will call a click function without clicking that. Here is an example:
是的,您可以使用 click 事件onLoad()
。只需使用setTimeout()
jquery中的方法。这将调用点击函数而不点击它。下面是一个例子:
$("document").ready(function() {
setTimeout(function() {
$("#add_redirect").trigger('click');
},10);
});
this will work for you when page load and time delay is 10ms which is negligible... Syntax has been corrected.
当页面加载和时间延迟为 10 毫秒(可以忽略不计)时,这对您有用……语法已更正。
回答by Ghyath Serhal
Try to add the below code on the page load
尝试在页面加载时添加以下代码
document.getElementById('add_redirect').click();