JQuery for mobile safari: on() fails

我怕爱的太早我们不能终老 提交于 2019-12-13 05:12:43

问题


I'm trying to change some classes on dynamically generated DOM elements for a mobile web page. My usual method, the jquery on() doesn't pick up the events properly.

Neither does "delegate()", my backup work.

I'm using JQuery version "http://code.jquery.com/jquery-1.7.min.js"


回答1:


The solution was to add onclick='' to the div element. Not sure why that worked, but it did. –




回答2:


Instead of using the on, can't you just write the event as it is?

Such as:

$(document).ready(function () {  });
$(document).click(function () {  });
$(document).keyup(function () {  });
$(document).keydown(function () {  });

?

Remember to add the events to the objects once the DOM is loaded. Cause if you try to use a jQuery selector outside a DOM ready check it might execute before the actual element is loaded and then it won't find anything. Which will result in no events happening.




回答3:


Try Live event as

$(".element").live("click",function(){
    alert("Clicked");
});

to attach event on dynamically generated DOM elements




回答4:


You need to make sure you are using jQuery 1.7.1 with jQuery Mobile 1.1.0 Final. It's important to be using version 1.7.1 of jQuery, not the 1.7.2 that is available right now as the latest.

Using on() in place of everything provides a much better experience. In fact, almost everything else has been deprecated.

I rambled a bit about this on a blog post a few weeks ago.

http://zsprawl.com/iOS/2012/04/using-new-jquery-1-7-on-off-functions/



来源:https://stackoverflow.com/questions/9440112/jquery-for-mobile-safari-on-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!