jQuery(移动)-将轻击事件绑定到div [英] jquery (mobile) - bind a tap event to a div

查看:118
本文介绍了jQuery(移动)-将轻击事件绑定到div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这个html:

If I have this html:

<div id="myDiv"></div>

和此CSS:

#myDiv{
    background:url('../images/someImage.png') no-repeat;
    background-size:100%;
    width:44px;
    height:44px;

}

当用户点击myDiv时,我需要打开一个新页面.我有一个外部js文件,其中有这个文件:

I need to open a new page when the user taps on myDiv. I have an external js file where I have this:

function bindMyDiv(){
    $("#myDiv").bind('tap',function(event, ui){
        alert("binding");
    })
}

但是我不知道从HTML哪里调用它,或者这是否是正确的解决方法.咨询吗?

But I don't understand where to call this from the HTML, or if this is even the right way to go about this. Advice?

推荐答案

这里的每个人都为您提供了有关脚本解决方案的很好的见解,但我认为没有人停下来想他们应该帮助您改善脚本吗? . 或者这是否是解决问题的正确方法",答案是否定的.也许我过于简化了,但是使用JQM,如果您尝试让div(或与此相关的任何DOM元素)打开新页面,只需在其周围(或在其中,以适当的方式为准)包裹一个定位标记即可. ),然后将href设置为href="#myNewPage",并将要加载到JcM页面上的ID设置为id="myNewPage"

Everyone here provided pretty good insight on different solutions for you to handle your script, but I don't think that anyone stopped to think SHOULD they help improve your script. "or if this is even the right way to go about this ", the answer is no. And perhaps I'm over-simplifying, but with JQM if you're trying to have a div (or any DOM element for that matter) open a new page simply wrap an anchor tag around around it (or in it, whichever is appropriate) and set your href to href="#myNewPage"and the id on the JQM page that you want to load to id="myNewPage"

jQuery Mobile的框架设置为自动注入JS&将AJAX转换为普通HTML元素可提供流畅的UX.尽管有时需要绑定触摸事件,但是这种情况并不能保证代码级别……这就是jQuery Mobile的优点=).

jQuery Mobile's frame work is set up to automatically inject JS & AJAX into normal HTML elements to provide a smooth UX. While binding a touch event is sometime needed, this situation doesn't warrant that level of code...thats the beauty of jQuery Mobile =).

何时绑定触摸事件的示例:显示/隐藏dom对象,触发对插件的单击等.

Examples of when to bind a touch event: show/hide a dom object, trigger a click for a plug-in etc.

这篇关于jQuery(移动)-将轻击事件绑定到div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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