JQuery mobile: applying style after reloading content

First of all, I am very new to jQuery and JavaScript, but not HTML and PHP.

I found a lot of posts about this problem, but could not find a solution. I think I'm doing something (completely?) Wrong ...

I have two files: index.php and content.php, which should dynamically add content to the page.

index.php:

<!DOCTYPE html> <html> <head xmlns="http://www.w3.org/1999/xhtml"> <meta charset="UTF-8"></meta> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link type="text/css" rel="stylesheet" href="../css/jquery.mobile-1.1.1.min.css"></link> <script type="text/javascript" src="../js/jquery-1.8.1.min.js"></script> <script type="text/javascript" src="../js/jquery.mobile-1.1.1.min.js"></script> <title>Ajax test</title> <script language="JavaScript"> $(function(){ $('#page1').click(function(event) { $('#content').load($(this).attr('href')).trigger("create"); return false; }); }); $(function(){ $('#page2').click(function(event) { $('#content').load($(this).attr('href')).trigger("create"); return false; }); }); </script> </head> <body> <div id="hm-agenda" data-theme="a" data-role="page"> <div data-theme="a" data-role="header"> <h1>Ajax test</h1> </div> <div id="content" data-role="content"> Default content </div> <div data-theme="a" data-role="footer" data-position="fixed" class="nav-glyphish-example"> <div data-role="navbar" class="nav-glyphish-example" data-grid="d" class="navdiv"> <ul> <li><a href="content.php?page=1" id="page1">Page 1</a></li> <li><a href="content.php?page=2" id="page2">Page 2</a></li> <li><a href="content.php?page=3" id="page3">Page 3</a></li> <li><a href="content.php?page=4" id="page4">Page 4</a></li> <li><a href="content.php?page=5" id="page5">Page 5</a></li> </ul> </div> </div> </body> </html> 

So, clicking on the page 1 or page 2 page in the navigation should load content.php? page = x in the div with the identifier "content".

And here is what content.php looks like:

 <ul data-role="listview" class="hm-list"> <li data-role="list-divider"><h3>You are on page: <?php print $_GET['page']; ?></h3></li> </ul> 

Uploading the file to the content div works fine if jQuery does not apply stylistic material. The strange thing is that after clicking the link in the navigation, the style is applied for about one second and immediately disappears.

This example is available at: http://m.sepulturagenda.ch/ajax/

Any help is appreciated. I stand here for several hours ...

+4
source share
2 answers

Your problem in this line:

 $('#content').load($(this).attr('href')).trigger("create"); 

You create an asynchronous request via .load() and do not .load() for it to complete before calling the .trigger("create") method. So basically you are calling the .trigger("create") method before there is any HTML to initialize.

You can use the callback function that you can pass to .load() to call the .trigger("create") method at the right time.

For instance:

 $(function(){ $('#page1').click(function(event) { $('#content').load($(this).attr('href'), function () { $('#content').trigger("create"); }).trigger("create"); return false; }); }); 

Brief note:

Is there a reason why you use .load() to load external content? jQuery Mobile will do this for you as long as the external content is in the correct format. Here are some documents to explain what I mean: http://jquerymobile.com/demos/1.1.1/docs/pages/page-links.html

And side note:

Using the document.ready event handler is not recommended when using jQuery Mobile. Instead, you should bind to one of the events of a jQuery Mobile page, such as pageinit .

For instance:

 //place this in the global scope $(document).on('pageinit', '#my-page', function () { $('#page1').click(function(event) { $('#content').load($(this).attr('href'), function () { $('#content').trigger("create"); }).trigger("create"); return false; }); }); 

Although, since you are using the identifier as a selector for the click event handler, you can delegate the event handler proxy to element #page1 .

For instance:

 $(document).on('click', '#page1', function () { $('#content').load($(this).attr('href'), function () { $('#content').trigger("create"); }).trigger("create"); return false; }); 

Here is the documentation for jQuery Mobile events (see the big yellow warnings at the top of the page): http://jquerymobile.com/demos/1.1.1/docs/api/events.html

+4
source

I am sure there is a better way to do this, but you can just tell it to add the class "ui-btn-active".

  $(function(){ $('#page2').click(function(event) { $(".ui-btn-active").removeClass("ui-btn-active"); $('#content').load($(this).attr('href')).trigger("create"); $(this).addClass('ui-btn-active'); return false; }); }); 
0
source

Source: https://habr.com/ru/post/1434945/


All Articles