jquery mobile needs a refresh to work properly

后端 未结 2 917
谎友^
谎友^ 2020-12-20 09:48

I wrote two pages using master page in .net and JQM. But when I redirect to second page, JQM swipe control does not works without a refresh. Besides, any control or function

相关标签:
2条回答
  • 2020-12-20 09:49

    Put the script call within the block:

    <div data-role="page" id="demo-page" data-theme="d">
    
    ... your script
    
    </div>
    

    If you're loading the pages via AJAX, the headers will be processed just once (or on refresh).

    0 讨论(0)
  • 2020-12-20 09:51

    How jQuery Mobile handles page changes

    To understand this situation you need to understand how jQuery Mobile works. It uses ajax to load other pages.

    First page is loaded normally. Its HEAD and BODY is loaded into the DOM, and they are there to await other content. When second page is loaded, only its BODY content is loaded into the DOM.

    That's why your button is show successfully but click event is not working. Same click event whose parent HEAD was disregarded during the page transition.

    Here's an official documentation: http://jquerymobile.com/demos/1.2.0/docs/pages/page-links.html

    Unfortunately you are not going to find this described in their documentation. Ether they think this is a common knowledge or they forgot to describe this like my other topics. (jQuery Mobile documentation is big but lacking many things).

    Solution 1

    In your second page, and every other page, move your SCRIPT tag into the BODY content, like this:

    <body>
        <div data-role="page">
            // And rest of your HTML content
            <script>
                // Your javascript will go here
            </script>
        </div>
    </body>
    

    This is a quick solution but still an ugly one.

    Working example can be found in my other answer here: [Pageshow not triggered after changepage][2]

    Another working example: [Page loaded differently with jQuery-mobile transition][3]

    Solution 2

    Move all of your javascript into the original first HTML. Collect everything and put it inside a single js file, into a HEAD. Initialize it after jQuery Mobile has been loaded.

    <head>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>    
        <script src="index.js"></script> // Put your code into a new file
    </head>
    

    In the end I will describe why this is a part of a good solution.

    Solution 3

    Use rel="external" in your buttons and every elements you are using to change page. Because of it ajax is not going to be used for page loading and your jQuery Mobile app will behave like a normal web application. Unfortunately this is not a good solution in your case.

    <a href="#second" class="ui-btn-right" rel="external">Next</a>
    

    Official documentation, look for a chapter: Linking without Ajax

    Realistic solution

    Realistic solution would use Solution 2. But unlike solution 2, I would use that same index.js file and initialize it inside a HEAD of every possible other page.

    0 讨论(0)
提交回复
热议问题