简体   繁体   English

JQuery $(document).ready(function()无效

[英]JQuery $(document).ready(function () not working

This is the first page where I try to open the detail.html; 这是我尝试打开detail.html的第一页;

<a href="detail.html" data-role="none" role="link">
    <div class="place">name</div>
    <div class="arrow"></div>
    <div class="ammount">-&euro;4,<span class="fontsize14">25</span></div>
</a>

I have problems to load the scripts on detail.html (after href link is clicked on first page) 我有问题在detail.html上加载脚本(在第一页上点击href链接后)

Here is my script in header of details.html(the page I go after href is clicked on first page), Problem is I can NOT get the console test print, that function is NOT called when details.html page is loaded. 这是我在details.html(页面HREF后我会去点击第一页),但问题是我不能让控制台打印测试,加载details.html页面时,该功能不叫的头脚本。 It only hits after I manually refresh the page 它只在我手动刷新页面后才会出现

<script>
    $(document).bind("pageinit", function(){//or $(document).ready(function ()   
          console.log('test');
    });
</script>

To understand your problem I think you need to first understand how jQuery Mobile "loads" external pages. 要了解您的问题,我认为您需要首先了解jQuery Mobile如何“加载”外部页面。 By default when you click a link to a separate HTML page JQM loads the first data-role="page" on that page and attaches it to the DOM of the first page, the thing is JQM only loads that page div and not any scripts etc. that are outside that container. 默认情况下,当您单击指向单独HTML页面的链接时,JQM会在该页面上加载第一个data-role =“page”并将其附加到第一页的DOM,但事情是JQM 加载该页面 div而不是任何脚本等容器外面的等等。

If you want to run code for a second page, you either need to include that script on your first page and use event delegation (since the second page is not part of the DOM yet) or include the script withing the second page's date-role="page" wrapper. 如果要为第二页运行代码,则需要在第一页上包含该脚本并使用事件委派(因为第二页不是DOM的一部分)或者包含具有第二页日期角色的脚本=“page”包装器。

Case in point in your case if you want to run code for your details page you either need to include the script on your first page for example assuming you have a div on your detail.html page like the following 在您的情况下,如果您想要为您的详细信息页面运行代码,您需要在第一页上包含脚本,例如假设您在detail.html页面上有div,如下所示

<div id="details" data-role="page"> ..rest of your content 
</div>

Then on your first page you could do the following 然后在您的第一页上,您可以执行以下操作

$(document).on('pageinit', '#details', function() {
  console.log('test');
});

Or alternatively you can include a script tag withing your "details" page wrapper. 或者,您可以包含带有“详细信息”页面包装器的脚本标记。

EDIT: 编辑:
As I mentioned this is the default behavior, however if you wish you can tell jQuery Mobile to do a full post when loading a second page by adding in data-ajax="false" or rel="external" to your link for example 正如我所提到的,这是默认行为,但是如果您希望通过在链接中添加data-ajax="false"rel="external"来加载第二页,可以告诉jQuery Mobile执行完整帖子,例如

<a href="detail.html" data-ajax="false" data-role="none" role="link">
    <div class="place">name</div>
    <div class="arrow"></div>
    <div class="ammount">-&euro;4,<span class="fontsize14">25</span></div>
</a>

The difference between data-rel="external" and data-ajax="false" is if the second page is basically semantic in that data-rel="external" should be used if the second page is on a different domain. data-rel="external"data-ajax="false"之间的区别在于,如果第二页基本上是语义的,则如果第二页在不同的域上,则应该使用data-rel="external"

I made you an working example: http://jsfiddle.net/Gajotres/Eqzd2/ 我给你做了一个工作的例子: http//jsfiddle.net/Gajotres/Eqzd2/

$("#second").live('pagebeforeshow', function () {
    console.log('This will only execute on second page!');
});

You can use on instead of live if you are using last version of jQuery. 如果您使用的是最新版本的jQuery,则可以使用on而不是live。

Also take a look at my article about event flow in jQM page transition: https://stackoverflow.com/a/14010308/1848600 另请查看我关于jQM页面转换中的事件流的文章: https ://stackoverflow.com/a/14010308/1848600

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

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