What I want to do is using JQuery Mobile I want to dynamically load content on #newPage depending on what option I clicked. Below is the code I tried that goes to #newPage, but does not load any content. I tried to give as much info as possible and commented line by line to what I wanted it to do.
Sample XML:
<parentOption>
<option1>
<name>Stuff</name>
</option1>
<potion2>
<name>More Stuff</name>
</potion2>
<option3>
<name>Even More Stuff</name>
</option3>
</parentOption>
Assume I have connected to the XML file via Ajax:
<script>
function parseXML(xml) { //Parse the XML
$('selected').click(function(e) { //Once the link is clicked
var selectedValue = $(this).value(); //Find the value of the clicked link
e.preventDefault(); //Refresh the new page
$(xml).find(selectedValue).each(function() { //take the XML info and select all of the elements equaling to the value of the selected link
var nameValue = $(this).parent(); //Find the parent of the selected element
$('#dynamicList').append('<li>' + $(nameValue).child('name').text() + '</li>'); //Write out the list on the new page
});
$('#dynamicList').listview('refresh');
});
}
</script>
<div data-role="page" id="home">
<div data-role="content">
<ul data-role="listview">
<li><a class="selected" value="option1" href="#newPage">Option 1</a></li>
<li><a class="selected" value="option1" href="#newPage">Option 2</a></li>
<li><a class="selected" value="option1" href="#newPage">Option 3</a></li>
</ul>
</div>
</div>
<div data-role="page" id="newPage">
<div data-role="content">
<ul id="dynamicList" data-role="listview">
</ul>
</div>
</div>
Here is the desired output after clicking the first link:
<div data-role="page" id="home">
<div data-role="content">
<ul data-role="listview">
<li><a class="selected" value="option1" href="#newPage">Option 1</a></li>
<li><a class="selected" value="option1" href="#newPage">Option 2</a></li>
<li><a class="selected" value="option1" href="#newPage">Option 3</a></li>
</ul>
</div>
</div>
<div data-role="page" id="newPage">
<div data-role="content">
<ul id="dynamicList" data-role="listview">
<li>Stuff</li>
</ul>
</div>
</div>
I made you a working example, take a look here: http://jsfiddle.net/Gajotres/AzXdT/ . Also I need to give you a warning, this example works only in jsFiddle because you can't load xml's from other domains. If you want to load xml from your domain (your hosting server) use this code:
$.ajax({
type: "GET",
url: "sites.xml",
dataType: "xml",
success: function(xml) {
}
});
If possible use JSON instead of XML. Reason, complex XML can be big in size with a lot of data overhead. JSON is created with idea to fix this problem. In case you want to try it I made you another example: http://jsfiddle.net/Gajotres/8uac7/ . This example can be used everywhere so feel free to play with it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.