div元素上的touchevent在window元素上工作时不起作用

时间:2014-02-18 09:20:33

标签: javascript jquery html css touch

这听起来可能是一个非常愚蠢的问题,但它让我疯狂。我有div我要追加touchevent。我就是这样做的:

$("#superContainer").bind('touchstart', function(event, passedName) {
    alert("touch start");
    //[more code]
});

这种方法不起作用。没有警报,没有。 div #supercontainer的属性为width: 100%; top: 0px

以下方法有效:

$(window).bind('touchstart', function(event, passedName) {
    alert("touch start");
    //[more code]
});

但我想避免将整个窗口绑定到事件。我正在使用最新版本的jQuery。

1 个答案:

答案 0 :(得分:1)

根据http://api.jquery.com/jQuery/,你应该使用

$(function() {
  // Document is ready
});

jQuery(function( $ ) {
  // Document is ready
});

在这种情况下哪个是问题

同时使用.on代替.bind

  

从jQuery 1.7开始,.on()方法是将事件处理程序附加到文档的首选方法。   https://api.jquery.com/bind/