javascript jQuery 自动点击链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7267044/
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-10-25 23:31:38  来源:igfitidea点击:

jQuery auto click on a link

javascriptjqueryclick

提问by Mihai Iorga

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 个小时,让我发疯了,我知道我一定是在做一些愚蠢的事情。

JsFiddlefor your convenience.

JsFiddle为了您的方便。

I wouldn't mind any other solution in plain JavaScript.

我不介意纯 JavaScript 中的任何其他解决方案。

回答by ThiefMaster

Use elem[0].click();instead of elem.click();since you want to call the native click function and not just trigger the click event.

使用elem[0].click();而不是elem.click();因为您想调用本机点击函数而不是仅仅触发点击事件。

By the way: Popup blockers will prevent this from actually opening a new window (luckily).

顺便说一句:弹出窗口阻止程序将阻止实际打开一个新窗口(幸运的是)。

回答by kmcc049

Simulating 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.

模拟用户物理点击链接是不可能的。由于您使用的是 target='_blank' 我想您想要一个新窗口?所以你需要使用window.open。哪些弹出窗口拦截器不会喜欢。

回答by Tomgrohl

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>

Demo here

演示在这里

回答by Samich

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();}
}