javascript 如何在网站加载时自动点击链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10817056/
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
How to automatically click a link when the website loads
提问by Deepak Kamat
I am trying to figure out how to make an anchor link clicked when the website is visited. I would be grateful if someone can help me, here's a demo code, you can match the id for my ease thank you.
我想弄清楚如何在访问网站时点击锚链接。如果有人可以帮助我,我将不胜感激,这是一个演示代码,您可以为我轻松匹配 ID,谢谢。
<body>
<a id='deepakkamat' href='http://mywebpage.com'> This is my webpage </a>
</body>
So i want to know any JavaScript code to make the link clicked when the page loads. Thank you in advance.
所以我想知道在页面加载时点击链接的任何 JavaScript 代码。先感谢您。
采纳答案by Joel Etherton
The jquery answer:
jQuery 的回答:
$($('#deepakkamat').click());
This will actually register a click event as the cause, but it seems awfully unnecessary. The meta method is the preferred method.
这实际上会将点击事件注册为原因,但这似乎非常没有必要。元方法是首选方法。
回答by woz
Use the meta tag to redirect to another page when the page loads:
页面加载时使用元标记重定向到另一个页面:
<meta http-equiv="refresh" content="2;url=http://www.example.com/" />
Where 2is the number of seconds before the redirect occurs.
2重定向发生前的秒数在哪里。
This will be more reliable and more straightforward than writing JavaScript to click a link. Let me know how it works for you.
这将比编写 JavaScript 来单击链接更可靠、更直接。让我知道它是如何为你工作的。
回答by fb55
When you're trying to archive a redirect, use document.location.href = "http://mywebpage.com"(or the meta tag mentioned before).
当您尝试存档重定向时,请使用document.location.href = "http://mywebpage.com"(或之前提到的元标记)。
There is also a clickmethod available, so that you can run
还有一种click方法可用,以便您可以运行
document.getElementById("deepakkamat").click()???;?

