javascript 如何使用javascript动态单击“a href链接”

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

how to click an "a href link" dynamically using javascript

javascriptjavascript-events

提问by sree

Possible Duplicate:
Is it possible to trigger a link's (or any element's) click event through JavaScript?

可能的重复:
是否可以通过 JavaScript 触发链接(或任何元素)的点击事件?

how to click an "a href link" dynamically using javascript

如何使用javascript动态单击“a href链接”

i am using this code to call javascript code

我正在使用此代码调用 javascript 代码

<a href="javascript:void(0)" onclick="javascript:execute();">

i want to click this link automatically

我想自动点击这个链接

is it possible with javascript?

可以用javascript吗?

回答by Shehzad Bilal

Give your href link some id then use this: document.getElementById('id').click();

给你的href链接一些id然后使用这个:document.getElementById('id').click();

回答by RobG

An A element is intended to be used as either a link or anchor, if you don't want either of those you should use some other element. In this case, a button seems the better option. Then users have a visual clue that the element will do something but not navigate anywhere.

A 元素旨在用作链接或锚点,如果您不想要其中任何一个,则应使用其他元素。在这种情况下,按钮似乎是更好的选择。然后用户有一个视觉线索,该元素将执行某些操作但不会导航到任何地方。

You can dispatch an event on the element (see xdazz's comment), or if you don't want the click to bubble you can just call the handler and set the element as this:

您可以在元素上调度一个事件(请参阅 xdazz 的评论),或者如果您不希望点击冒泡,您可以调用处理程序并将元素设置为this

element.onclick.call(element);