让JS在JQM页面上加载

时间:2013-04-03 05:20:59

标签: jquery jquery-mobile jquery-plugins jquery-mobile-pageshow

我正在尝试在JQM上运行一个插件,但我需要它在第二页处于活动状态时等待并加载。现在,它在启动时加载。我的页面设置为data-role='page'。这是我的代码:

$(document).ready(function(){
    $("#two").bind('pageinit')
    jQuery(".namesleft").fitText();
    jQuery(".scoreleft").fitText();
    jQuery(".namesright").fitText();
    jQuery(".scoreright").fitText();
});

提前感谢您的帮助

3 个答案:

答案 0 :(得分:1)

jQuery mobile可以替代$(document).ready。 代码看起来像这样:

    <script type="text/javascript">
       $('div:jqmData(role="page")').live('pagebeforeshow',function(){
             // code to execute on each page change
       });
</script>

实际上,jQm有一整套事件替代常规jQuery事件: http://jquerymobile.com/demos/1.2.1/docs/api/events.html

答案 1 :(得分:1)

这是一个有效的jsFiddle示例:http://jsfiddle.net/Gajotres/F2Gcm/

$(document).on('pageshow', '#index', function(){       
    $("h1").fitText(0.273);
    $(".download").fitText(1);
});
应该使用

On 方法代替 live 方法来绑定页面事件,不推荐使用 live 方法,并且jQuery中不存在1.9 +。

此外,这应该绑定到 pageshow 事件,因为它不会在任何其他情况下工作。 jQuery Mobile对插件进行视觉更改非常严格,这种插件只能在 pageshow 事件中使用。

如果您想更好地了解jQuery Mobile页面事件,请查看 ARTICLE ,或者找到 HERE

答案 2 :(得分:0)

您可以在JQM的pageshow事件中添加您的代码,例如:

$("#two").live('pageshow', function(){
    $(".namesleft, .scoreleft, .namesright, .scoreright").fitText();
});