我想要做的是使用 JQuery Mobile 我想根据我单击的选项在#newPage 上动态加载内容。下面是我尝试过的#newPage 代码,但不加载任何内容。我试图提供尽可能多的信息,并逐行评论我想要它做的事情。
示例 XML:
<parentOption>
<option1>
<name>Stuff</name>
</option1>
<potion2>
<name>More Stuff</name>
</potion2>
<option3>
<name>Even More Stuff</name>
</option3>
</parentOption>
假设我已经通过 Ajax 连接到 XML 文件:
<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>
这是单击第一个链接后所需的输出:
<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>