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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:59:35  来源:igfitidea点击:

can I trigger click event onload

javascriptjquery

提问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 clickevent synthetically probably won't work. Browsers are smart enough to know when a clickis user-generated vs. when you've called the clickfunction 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/3http://jsbin.com/avibi3/4

Using jQuery's triggermechanism certainly won't do it, because it doesn't really trigger a clickevent at all; it just fires the handlers that jQuery hooked up (edit: and, apparently, ones defined via an onclickattribute — 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 clickhandler 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();

回答by Sukhjeevan

Using JQuery you can do that easily.well... earlier posted solution also worked.

使用 JQuery 你可以很容易地做到这一点。好吧......早先发布的解决方案也有效。

$(document).ready(function(){
 $("#add_redirect").trigger('click');
});

TRY DEMO

试用演示