以编程方式触发onClick事件? [英] Programatically trigger an onClick event?

查看:68
本文介绍了以编程方式触发onClick事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用JavaScript使浏览器点击

页面上的链接,让浏览器的行为与用户点击链接完全相同

直接?


换句话说,我需要以编程方式发出一个JavaScript语句

这会导致浏览器的行为就像用户点击我的链接一样

页面。如果该链接定义了一个onClick JS事件,我也希望执行这个
onClick事件,就像用户实际上点击了b
$ b一样链接。


我知道您可能会认为我应该直接调用

链接的onClick事件,而不是尝试使用JavaScript模拟点击。

然而这还不够。在我的情况下,我需要使用JS导致

浏览器点击链接,就像用户点击它一样,有任何

相应的事件可能被定义为fire(例如onClick),然后

让浏览器按照href链接到其相应的

目标/页面。可以把它想象成一个类似宏的操作我想做的事情

我正在模拟点击链接的最终用户的动作。


有人可以告诉我这是否可能,如果是的话,怎么样?我需要它以

使用IE 5及更高版本,以及更高版本的Netscape / Opera / Mozilla

如果可能的话。


我想我可能能够以编程方式访问href

标签的属性,并在其上调用一个方法来模拟真正的点击。任何

代码样本或片段都会非常受欢迎。


谢谢!!


** Mike

解决方案

Mike Gratee写道:

是否可以使用JavaScript导致浏览器点击链接<在一个页面上,让浏览器的行为与用户在链接上直接点击的行为完全相同?




var link = document.getElementById [' 'yourLinksIdAttrbuteValue''];

link.click();


-

如果互联网是马克斯兄弟电影,网络,电子邮件和IRC是

Groucho,Chico和Harpo,然后Usenet是Zeppo。


很棒 - 非常感谢。但是我认为这不适用于Netscape 6,

Mozilla或Opera对吗?如何改变它与它们一起工作?


迈克


" Berislav Lopac" <是************ @ dimedia.hr>在消息中写道

news:cd ********** @ ls219.htnet.hr ...

Mike Gratee写道:

是否可以使用JavaScript使浏览器点击页面上的链接
并使浏览器的行为与用户在链接上直接点击
直接相同?



var link = document.getElementById [''yourLinksIdAttrbuteValue''];
link.click();

-
如果互联网是马克思兄弟电影,网络,电子邮件和IRC是Groucho,Chico和Harpo,然后Usenet是Zeppo。



< blockquote> Mike Gratee写道:

var link = document.getElementById [''yourLinksIdAttrbuteValue''];
link.click();



非常好 - 非常感谢。但是我认为这不适用于Netscape 6,Mozilla或Opera吗?我如何改变它与它们一起工作?




你试过吗?如果没有,为什么你认为它不工作?如果是的话,

到底发生了什么?


Berislav


-

如果互联网是马克斯兄弟电影,网络,电子邮件和IRC是Groucho,Chico和Harpo,那么Usenet就是Zeppo。

Is it possible to use JavaScript to cause the browser to click a link on a
page and have the browser act exactly like the user had clicked on the link
directly?

In other words, I need to programmatically issue a JavaScript statement
which causes the browser to act just like the user clicked on a link in my
page. And if that link has an onClick JS event defined, I''d want that
onClick event to execute too, exactly the same as if the user actually
clicked on the link.

I know you may be thinking that I should just call the onClick event for the
link directly rather than trying to simulate a click using JavaScript.
However this is not sufficient. In my case I need to use JS to cause the
browser to click a link just like the user had clicked it, have any
corresponding events that may be defined fire (such as onClick), and then
have the browser follow the href for the link to its appropriate
target/page. Think of it as a macro-like operation I am trying to do
whereby I am simulating an action of and end user that is clicking a link.

Can someone let me know if this is possible, and if so, how? I need it to
work with IE 5 and higher, and the later versions of Netscape/Opera/Mozilla
if possible.

I''m thinking that someone I may be able to access the properties of an href
tag programmatically and call a method on it to simulate a real click. Any
code samples or snippets would be MUCH appreciated.

Thanks!!

** Mike

解决方案

Mike Gratee wrote:

Is it possible to use JavaScript to cause the browser to click a link
on a page and have the browser act exactly like the user had clicked
on the link directly?



var link = document.getElementById[''yourLinksIdAttrbuteValue''];
link.click();

--
If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
Groucho, Chico, and Harpo, then Usenet is Zeppo.


Great - thanks alot. However I don''t think that will work with Netscape 6,
Mozilla or Opera right? How would I change it to work with those?

Mike

"Berislav Lopac" <be************@dimedia.hr> wrote in message
news:cd**********@ls219.htnet.hr...

Mike Gratee wrote:

Is it possible to use JavaScript to cause the browser to click a link
on a page and have the browser act exactly like the user had clicked
on the link directly?



var link = document.getElementById[''yourLinksIdAttrbuteValue''];
link.click();

--
If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
Groucho, Chico, and Harpo, then Usenet is Zeppo.



Mike Gratee wrote:

var link = document.getElementById[''yourLinksIdAttrbuteValue''];
link.click();



Great - thanks alot. However I don''t think that will work with
Netscape 6, Mozilla or Opera right? How would I change it to work
with those?



Have you tried? If not, why do you think if it won''t work? If yes, what
exactly happens?

Berislav

--
If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
Groucho, Chico, and Harpo, then Usenet is Zeppo.


这篇关于以编程方式触发onClick事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆