5

I try to link virtual pages using jQuery Mobile, but I have two problems :

  • The first time when I load the page, the CSS is not applied.
  • After when I choose a page and I want to change to another page, I notice that every time I passed by the page 1.

This is my example.

Code :

            var nbrButton = 3;
            $(document).ready(function(){
                for(i = 1;i <= nbrButton;i++) {

                    $("body").append('<div id="p'+i+'" data-role="page" class="pages"><div data-role="content">Page'+i+'</br><a data-role="button" rel="internal" href="#p1"  data-inline="true">1</a><a data-role="button" rel="internal" href="#p2"  data-inline="true">2</a><a data-role="button" rel="internal" href="#p3"  data-inline="true">3</a></div></div>');

                }
                $("#p1").show();

            });

Could you tell me what is the problem or if there is a better way to do that.

Thank you.

3 Answers 3

2

Update

I also removed data-rel="internal" in the links.

Answer

I have done the below.

instead of

$('#p1').show();

I add this

$.mobile.changePage( '#p1', { allowSamePageTransition: true });

It will refresh Page-1 p1 to reload styles.

Working example.

Sign up to request clarification or add additional context in comments.

8 Comments

Great ! it remains the second problem. Thank you.
Page 1 won't appear when moving to other pages. is this your second problem?
yes ! I forgot to remove $('#p1').show() شكراً جزيلا عمر
Remove data-rel="internal" Afwan akhi :) (you're welcome in Arabic)
Great :) I'm glad I've been of help.
|
2

You can't dynamically create new jQuery Mobile page (you can, but that page will not have styles) unless you have at least one existing. There's a workaround here, you can create an empty jQuery mobile page and use it to create a new one.

Here's a working example:

<!DOCTYPE html>
<html>
<head>
    <title>jQM Complex Demo</title>
    <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://www.dragan-gaic.info/js/jquery-1.8.2.min.js"></script>        
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>    
    <script>
                $(document).on('pageshow', '#index', function(){       
                    $('<div>').attr({'data-role':'page','id':'second'}).appendTo('body');
                    $('<div>').attr({'data-role':'header','id':'second-header'}).append('<h3>Header Title</h3>').appendTo('#second');
                    $.mobile.changePage("#second");
                });    
    </script>
</head>
<body>
    <div data-role="page" id="index">

    </div>  
</body>
</html>   

Now you should use a pageshow page event of a first hidden page to create new dynamic pages. After page are cretaed just use change page to show a first visible page.

2 Comments

The problem is that I don't know in advance the number of pages.. Thank you.
Why is this a problem in case of my example? Just copy your code inside pageshow example.
-1

To apply CSS styles just add $("#p1").trigger('create'); as last line after $("#p1").show();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.