如何使用 JavaScript 以编程方式单击链接?

发布于 2024-07-23 02:53:08 字数 37 浏览 7 评论 0原文

有没有办法使用 JavaScript 单击我的页面上的链接?

Is there a way to click on a link on my page using JavaScript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

烧了回忆取暖 2024-07-30 02:53:08
document.getElementById('yourLinkID').click();
document.getElementById('yourLinkID').click();
面犯桃花 2024-07-30 02:53:08

如果您只想更改当前页面地址,只需在 Javascript 中执行以下操作即可:

location.href = "http://www.example.com/test";

If you only want to change the current page address, you can do that by simply doing this in Javascript :

location.href = "http://www.example.com/test";
阳光下慵懒的猫 2024-07-30 02:53:08

该功能至少可以在 Firefox 和 Internet Explorer 中使用。 如果事件处理程序不取消默认操作,它会运行附加到链接的任何事件处理程序并加载链接的页面。

function clickLink(link) {
    var cancelled = false;

    if (document.createEvent) {
        var event = document.createEvent("MouseEvents");
        event.initMouseEvent("click", true, true, window,
            0, 0, 0, 0, 0,
            false, false, false, false,
            0, null);
        cancelled = !link.dispatchEvent(event);
    }
    else if (link.fireEvent) {
        cancelled = !link.fireEvent("onclick");
    }

    if (!cancelled) {
        window.location = link.href;
    }
}

This function works in at least Firefox, and Internet Explorer. It runs any event handlers attached to the link and loads the linked page if the event handlers don't cancel the default action.

function clickLink(link) {
    var cancelled = false;

    if (document.createEvent) {
        var event = document.createEvent("MouseEvents");
        event.initMouseEvent("click", true, true, window,
            0, 0, 0, 0, 0,
            false, false, false, false,
            0, null);
        cancelled = !link.dispatchEvent(event);
    }
    else if (link.fireEvent) {
        cancelled = !link.fireEvent("onclick");
    }

    if (!cancelled) {
        window.location = link.href;
    }
}
逆夏时光 2024-07-30 02:53:08

就像这样:

<a id="myLink" onclick="alert('link click');">LINK 1</a>
<a id="myLink2" onclick="document.getElementById('myLink').click()">Click link 1</a>

或者在页面加载时:

<body onload="document.getElementById('myLink').click()">
...
<a id="myLink" onclick="alert('link click');">LINK 1</a>
...
</body>

Simply like that :

<a id="myLink" onclick="alert('link click');">LINK 1</a>
<a id="myLink2" onclick="document.getElementById('myLink').click()">Click link 1</a>

or at page load :

<body onload="document.getElementById('myLink').click()">
...
<a id="myLink" onclick="alert('link click');">LINK 1</a>
...
</body>
苍景流年 2024-07-30 02:53:08

单击链接的 jQuery 方法是

$('#LinkID').click();

对于 mailTo 链接,您必须编写以下代码

$('#LinkID')[0].click();

The jQuery way to click a link is

$('#LinkID').click();

For mailTo link, you have to write the following code

$('#LinkID')[0].click();
夏见 2024-07-30 02:53:08

对我来说,我成功地做到了这一点。 我在 5000 毫秒内部署了自动点击,然后在 1000 毫秒后关闭了循环。 然后就只有1次自动点击。

<script> var myVar = setInterval(function ({document.getElementById("test").click();}, 500)); setInterval(function () {clearInterval(myVar)}, 1000));</script>

For me, I managed to make it work that way. I deployed the automatic click in 5000 milliseconds and then closed the loop after 1000 milliseconds. Then there was only 1 automatic click.

<script> var myVar = setInterval(function ({document.getElementById("test").click();}, 500)); setInterval(function () {clearInterval(myVar)}, 1000));</script>
反差帅 2024-07-30 02:53:08

上述许多方法已被弃用。 现在建议使用此处找到的构造函数

function clickAnchorTag() {
    var event = document.createEvent('MouseEvent');
    event = new CustomEvent('click');
    var a = document.getElementById('nameOfID');
    a.dispatchEvent(event);
}

这将导致锚标记被单击,但它不会显示弹出窗口阻止程序是否处于活动状态,因此用户需要允许弹出窗口。

Many of the above methods have been deprecated. It is now recommended to use the constructor found here

function clickAnchorTag() {
    var event = document.createEvent('MouseEvent');
    event = new CustomEvent('click');
    var a = document.getElementById('nameOfID');
    a.dispatchEvent(event);
}

This will cause the anchor tag to be clicked, but it wont show if pop-up blockers are active so the user will need to allow pop-ups.

谁的年少不轻狂 2024-07-30 02:53:08

您可以使用 Javascript 转发到点击将转到的 URL,而不是单击吗?

也许你可以把一些东西放在 onLoad 的主体中去你想要的地方。

Instead of clicking, can you forward to the URL that the click would go to using Javascript?

Maybe you could put something in the body onLoad to go where you want.

Saygoodbye 2024-07-30 02:53:08

您可以将它们重定向到另一个页面。 事实上,从字面上点击一个链接并前往它似乎是不必要的,但我不知道整个故事。

You could just redirect them to another page. Actually making it literally click a link and travel to it seems unnessacary, but I don't know the whole story.

丿*梦醉红颜 2024-07-30 02:53:08

对于那些想要单击具有特定文本内容的所有链接的人,这可行:

for (const a of document.querySelectorAll("a")) {
  if (a.textContent.includes("<your text to be searched here>")) {
    a.click();
  }
}

参考:https://stackoverflow.com/a/42907920/2361131

for those wanting to click all links that have a particular text content, this would work:

for (const a of document.querySelectorAll("a")) {
  if (a.textContent.includes("<your text to be searched here>")) {
    a.click();
  }
}

reference: https://stackoverflow.com/a/42907920/2361131

却一份温柔 2024-07-30 02:53:08

客户端 JS 函数在以下情况下自动单击链接...

这是一个示例,您检查隐藏表单输入的值,该输入包含从服务器传递下来的错误...然后您的客户端 JS 检查它的值并在您指定的另一个位置填充错误。在本例中是弹出登录模式。

var signUperror = document.getElementById('handleError')

if (signUperror) {
  if(signUperror.innerHTML != ""){
  var clicker = function(){
    document.getElementById('signup').click()
  }
  clicker()
  }
}

Client Side JS function to automatically click a link when...

Here is an example where you check the value of a hidden form input, which holds an error passed down from the server.. your client side JS then checks the value of it and populates an error in another location that you specify..in this case a pop-up login modal.

var signUperror = document.getElementById('handleError')

if (signUperror) {
  if(signUperror.innerHTML != ""){
  var clicker = function(){
    document.getElementById('signup').click()
  }
  clicker()
  }
}
澜川若宁 2024-07-30 02:53:08

你不能让用户的鼠标做任何事情。 但您可以完全控制事件触发时发生的情况。

您可以做的就是点击身体负荷。 W3Schools 有一个示例此处。

You can't make the user's mouse do anything. But you have full control over what happens when an event triggers.

What you can do is do a click on body load. W3Schools has an example here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文