【问题标题】:Trigger click event on link tag in JS for Android and iPhone在 Android 和 iPhone 的 JS 中的链接标签上触发点击事件
【发布时间】:2013-01-29 16:48:55
【问题描述】:

我正在使用 Sencha Touch(JavaScript 和 ExtJS)开发移动 Web 应用程序。我有一个链接标签(只是带有href<a> 标签),我需要在上面触发click 事件。
我发现myelement.dom.click(); 的调用可以完成这项工作,但它不适用于 Android... 是否有某种通用解决方案?

更新

我有一个带有 html 代码的容器:

<a href="http://google.com" id="link_to_click" target="_blank">CLICK ME</a>

我只需要使用普通的 JavaScriptExtJS 来模拟点击此链接。该链接必须在新窗口/标签中打开。

【问题讨论】:

  • 详细描述您的问题。您在 Android 设备上使用的是哪个浏览器。哪个安卓。在这里写不工作的代码。

标签: javascript android iphone extjs sencha-touch-2


【解决方案1】:

使用HTMLEvents 对象和document.createEvent() 来模拟对链接的点击。

var link = document.getElementById( 'link_to_click' ),
    event = document.createEvent( 'HTMLEvents' );

event.initEvent( 'click', true, true );
link.dispatchEvent( event );

【讨论】:

  • 这是天才,多年来一直在寻找解决方案,干杯! :)
【解决方案2】:

//安卓

WebView wv = (WebView) findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);

//javascript

function bindIcons() {

            jQuery("a.icons").bind('mouseover mouseout mousedown mouseup focus blur touchstart touchend', function (e){
              if (typeof console !== "undefined" ) console.log(e);
              if (e.type=='mouseover' || e.type=='focus') 
              {
                jQuery(e.currentTarget).toggleClass('over',true);
              }

              if (e.type=='mousedown' || e.type=='touchstart') 
              {
                jQuery(e.currentTarget).toggleClass('down',true);
              }

              if (e.type=='mouseout' || e.type=='mouseup' || e.type=='touchend' || e.type=='blur') 
              {
                jQuery(e.currentTarget).toggleClass('down',false);
                jQuery(e.currentTarget).toggleClass('over',false);
              }
            });

            if (isAndroid())
               jQuery("a.ifandroid").attr("href","javascript:ifAndroid();");
            else
               jQuery("a.ifandroid").toggleClass('ifandroid',false);

      }

【讨论】:

  • 我无法使用 jQuery。我只能使用纯 JavaScript 和 ExtJS。而且这是事件绑定,但是我需要事件触发(点击模拟)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多