javascript 如何在 jQuery Mobile 中绑定“mobileinit”事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10214083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to bind "mobileinit" event in jQuery Mobile?
提问by aligf
This is how I'm trying to hook into the mobileinit event:
这就是我试图挂钩 mobileinit 事件的方式:
$(document).bind("mobileinit", function() {
console.log("Mobile init");
});
But this doesn't work on Chrome (Latest version), Ripple v0.9.1 and on a BlackBerry bold 9790 running OS7.0.
但这不适用于 Chrome(最新版本)、Ripple v0.9.1 和运行 OS7.0 的 BlackBerry bold 9790。
Note: I also tried using .on()
instead of .bind()
but no luck. Both jQuery mobile versions (1.0.1 and 1.1.0) failed.
注意:我也尝试使用.on()
而不是.bind()
但没有运气。两个 jQuery 移动版本(1.0.1 和 1.1.0)都失败了。
回答by CWSpear
I've used this and it doeswork.
我用过这个,它确实有效。
Is it possible something else is breaking the script or the mobileinit isn't being fired?
是否有其他原因破坏了脚本或没有触发 mobileinit?
Does Chrome fire mobileinit?
Chrome 会触发 mobileinit 吗?
I just found some code I used in jQuery Mobile 1.0 and we just upgraded to 1.1.0 and it works.
我刚刚找到了一些我在 jQuery Mobile 1.0 中使用的代码,我们刚刚升级到 1.1.0 并且它可以工作。
You're making sure to also include regular ol' jQuery, right?
您确保还包含常规的 ol' jQuery,对吗?
jQueryMobile's docs do it, so I'm sure it works. Something else must be wrong. Sorry I'm not much help. Do you have any more info? Or try with a different device.
jQueryMobile's docs do it,所以我确定它有效。一定是别的什么地方出了问题。抱歉,我帮不上什么忙。你有更多信息吗?或者尝试使用其他设备。
[edit]On that same self page, it says "Because the mobileinit event is triggered immediately, you'll need to bind your event handler before jQuery Mobile is loaded. Link to your JavaScript files in the following order:"
[编辑]在同一个 self 页面上,它说“因为 mobileinit 事件是立即触发的,您需要在加载 jQuery Mobile 之前绑定您的事件处理程序。按以下顺序链接到您的 JavaScript 文件:”
<script src="jquery.js"></script>
<script src="custom-scripting.js"></script> <!-- Note your script before jqm -->
<script src="jquery-mobile.js"></script>
Looks like the script order can matter.
看起来脚本顺序很重要。
回答by Samy Omar
Here is another simple example that works with me (for android and ios)
这是另一个适用于我的简单示例(适用于 android 和 ios)
<script type="text/javascript" src="files/resources/lib/jquery/jquery-1.8.2.js"> </script>
<script type="text/javascript">
$(document).bind("mobileinit", function()
{
if (navigator.userAgent.toLowerCase().indexOf("android") != -1)
{
// your logic here
$.mobile.defaultPageTransition = 'none';
$.mobile.defaultDialogTransition = 'none';
}
if (navigator.userAgent.toLowerCase().indexOf("msie") != -1)
{
// your logic here
$.mobile.allowCrossDomainPages = true;
$.support.cors = true;
}
});
</script>
<script type="text/javascript" src="files/resources/lib/jquerymobile/1.3.2/jquery.mobile-1.3.2.js"></script>
- include the main jquery file
- bind mobileinit before jquery mobile
- include jquery mobile js file
- 包含主要的 jquery 文件
- 在 jquery mobile 之前绑定 mobileinit
- 包含 jquery 移动 js 文件