简体   繁体   English

移动Safari,jQuery和绑定到未来的DOM元素

[英]Mobile Safari, jQuery, and binding to future DOM elements

I have an HTML5/JavaScript app built for a car in-dash display unit that I have been asked to port over to run in the browser. 我有一个HTML5 / JavaScript应用程序,用于汽车仪表板显示单元,我被要求移植到浏览器中运行。

Easy task, made some simple changes and now everything works fine in every desktop browser that I tried in. 简单的任务,做了一些简单的更改,现在一切正常,我尝试过的每个桌面浏览器。

Then, came problems. 然后,出现了问题。 I was asked to specifically make sure it could run on an iPad for demonstration purposes. 我被要求专门确保它可以在iPad上运行以用于演示目的。

THE ISSUE: 问题:

The app uses single page navigation, using jQuery.load to load in the necessary page fragments as the user navigates to different 'screens.' 该应用程序使用单页面导航,当用户导航到不同的“屏幕”时,使用jQuery.load加载必要的页面片段。 However, all of the jQuery bindings are centralized in one place so that they are performed on app startup, even though the elements they are binding to may not yet exist in the DOM. 但是,所有jQuery绑定都集中在一个位置,以便它们在应用程序启动时执行,即使它们绑定到的元素可能尚未存在于DOM中。

That's fine using jQuery.live or .on since they allow binding to future DOM elements. 使用jQuery.live或.on很好,因为它们允许绑定到未来的DOM元素。 At least in desktop browsers. 至少在桌面浏览器中。

In Mobile Safari, .live/.on simply do not work for binding to elements that do not yet exist. 在Mobile Safari中,.live / .on根本不适用于绑定到尚不存在的元素。

I have an example to illustrate this problem here: http://dhines.com/jquery-test/ 我有一个例子来说明这个问题: http//dhines.com/jquery-test/

Notice that all of the 'SHOULD WORK' links do work in desktop browsers, but not Mobile Safari! 请注意,所有'SHOULD WORK'链接都可以在桌面浏览器中使用,但不适用于Mobile Safari!

Looking around the web and SO, the only solution I have found would be to add onclick="function(){}" to all of my clickable elements. 环顾网络和SO,我发现的唯一解决方案是将onclick =“function(){}”添加到我的所有可点击元素中。 This seems dumb. 这看起来很愚蠢。

The other option I see would be to decentralize my bindings and have them take place on the specific fragments that they apply to, but given the ton of fragments and bindings, this would not be fun. 我看到的另一个选择是分散我的绑定并将它们发生在它们适用的特定片段上,但是由于大量的片段和绑定,这将不会很有趣。

Looking at my example, can anyone provide me with a better solution? 看看我的例子,有人能为我提供更好的解决方案吗? Or perhaps point out whether I'm using .load incorrectly or something? 或者也许指出我是否正在使用.load错误或其他什么?

I can not test this (I have no apple product), but a I have found a discussion on the jquery ticket system: 我无法测试这个(我没有苹果产品),但我找到了关于jquery票证系统的讨论:

http://bugs.jquery.com/ticket/5677#comment:7 http://bugs.jquery.com/ticket/5677#comment:7

the trick seems to be to add { cursor : pointer } to the elements I have seen that your example doesn't define this css property. 诀窍似乎是将{cursor:pointer}添加到我看到的元素中,你的例子没有定义这个css属性。 Give this a try... 试一试......

Maybe Apple Safari developer thought that an click event doesn't make sense on an element which doesn't have curosr:pointer. 也许Apple Safari开发人员认为单击事件对没有curosr:指针的元素没有意义。

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

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