在Android和iPhone的JS中触发链接标签上的点击事件 [英] Trigger click event on link tag in JS for Android and iPhone
问题描述
我正在使用 Sencha Touch(JavaScript 和 ExtJS)开发移动网络应用程序.我有一个链接标签(只是带有 href
的 标签),我需要在它上面触发
click
事件.
我发现调用 myelement.dom.click();
可以完成这项工作,但它不适用于 Android... 有什么通用的解决方案吗?
I am developing mobile web application using Sencha Touch (JavaScript & ExtJS). I have a link tag (just <a>
tag with href
), and I need to trigger click
event on it.
I have found that call of myelement.dom.click();
do the job, but it's not working for Android... Is there some kind of universal solution?
更新
我有一个带有 html 代码的容器:
I have a container with html code:
<a href="http://google.com" id="link_to_click" target="_blank">CLICK ME</a>
我需要仅使用普通的 JavaScript 和 ExtJS 来模拟点击此链接.链接必须在新窗口/标签中打开.
I need to simulate click on this link using only plain JavaScript and ExtJS. The link must be opened in new window/tab.
推荐答案
使用 HTMLEvents
对象和 document.createEvent()
来模拟点击链接.
Use the HTMLEvents
object with document.createEvent()
to simulate a click on a link.
var link = document.getElementById( 'link_to_click' ),
event = document.createEvent( 'HTMLEvents' );
event.initEvent( 'click', true, true );
link.dispatchEvent( event );
这篇关于在Android和iPhone的JS中触发链接标签上的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!