简体   繁体   English

动态添加的链接上的trigger('click')对于JQM中的外部链接不会传播

[英]trigger('click') on a dynamically added link does not propagate for external links in JQM

I am attempting to use jQuery Mobile's Select Menu in a Navbar as a navigation tool. 我正在尝试使用导航栏中的jQuery Mobile的“选择菜单”作为导航工具。 It will need to handle both internal and external links. 它将需要处理内部和外部链接。 Binding to change enables the use of the $.mobile.selectmenu() widget to fetch the selected link which has been built with custom data bits for the URL and locality status. 通过绑定change可以使用$.mobile.selectmenu()小部件来获取所选链接,该链接已使用URL和位置状态的自定义数据位构建。 The link is then dynamically appended to the document and .trigger('click') is called. 然后将链接动态附加到文档,并.trigger('click')

http://jsfiddle.net/wZNMz/ http://jsfiddle.net/wZNMz/

Regardless of the permutations of binding events, return values, event propagation wrangling, and attributes the external link will not work. 无论绑定事件,返回值,事件传播是否发生争执以及属性如何排列,外部链接均不起作用。 The internal link works just fine. 内部链接工作正常。

Referring to the jquery.mobile.navigation.js source triggering click should work as expected assuming the [rel='external'] is matched to enable default URL handling. 假设匹配[rel='external']以启用默认URL处理, 参考jquery.mobile.navigation.js源触发单击,以预期的方式工作。 Note that the plugin strips links out of <option> tags on init requiring all the hackiness of adding a link and triggering click on a <select> change. 需要注意的是插件条链接出<option>上需要添加一个链接,并在触发点击的所有hackiness初始化标签<select>改变。

The dirty and obvious hack is to set window.location when external and be done. 肮脏而明显的技巧是在外部完成时设置window.location This does work but I can't see why it should be necessary to resort to that. 这确实有效,但是我看不出为什么有必要诉诸于此。 Any thoughts or insights are appreciated! 任何想法或见解都表示赞赏!

Creating a link and triggering a click on it seems a lot more hacky than using window.location , which is there for this specific reason, to forward users to a new page. 创建链接并触发点击似乎比使用window.location (因为这个特定原因)将用户转发到新页面要困难得多。 For internal links you can route the request through $.mobile.changePage() . 对于内部链接,您可以通过$.mobile.changePage()路由请求。 Here is an example: 这是一个例子:

$('#mobile-nav').bind('change', function(e) {
    var nav_to = $(this).selectmenu('selected');

    if (nav_to.data('locality') === false) {
        //this is external, so use window.location
        window.location = nav_to.data('item-url');
    } else {
        //this is internal, so use $.mobile.changePage()
        $.mobile.changePage(nav_to.data('item-url'), {
            //here you can set options such as transition type and direction
        });
    }
});

Here is the documentation for $.mobile.changePage() : http://jquerymobile.com/demos/1.1.1/docs/api/methods.html 这是$.mobile.changePage()的文档: http : //jquerymobile.com/demos/1.1.1/docs/api/methods.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM