我正在尝试使用 jQuery Mobile 解析 XML 文件。这是我的 jQuery 脚本:
<script>
$(document).ready(function(){
$.ajax({
type: "GET",
url: "bars.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('bar').each(function(){
var date = new Date();
var day = date.getDay();
var name = $(this).attr('name');
var id = $(this).attr('id');
var specials = $(this).find('specials').text();
if (id == day)
{
$('<li data-role="list-divider" id="link_'+id+'"></li>').html(name).appendTo('#daily-specials');
$('<li class="specials"></li>').html(specials).appendTo('#link_'+id);
}
});
});
});
});
</script>
还有我的 XML 文件:
<?xml version="1.0" encoding="iso-8859-1"?>
<bars>
<!-- Friday specials for bar one -->
<bar name="Bar One" id="5">
<specials>
Special 1 <br />
Special 2 <br />
Special 3 <br />
Special 4 <br />
Special 5
</specials>
</bar>
<!-- Saturday specials for bar one -->
<bar name="Bar One" id="6">
<specials>
Special 1 <br />
Special 2 <br />
Special 3 <br />
Special 4 <br />
Special 5
</specials>
</bar>
</bars>
谁能看出我做错了什么?如您所见,我正在尝试获取一周中当天的特价“Bar One”,然后将该信息传递到我页面上的 HTML 类。虽然我的网站上没有打印出来。如果我什至不接近,我深表歉意,但方向会有所帮助。谢谢!
最佳答案
正如您所说,网站上没有打印任何内容。 我看到您正在使用本地 xml 文件。同源政策可能会受到影响。 我猜你使用的是 chrome 浏览器。 (有关在 chrome 上使用 F12 键的错误,请参阅控制台。)
在 firefox 或 IE 上试试。
如果您仍然想使用 chrome.. 请在下面添加到 chrome 快捷方式的 targer 属性中。 --allow-file-access-from-files
希望这对您有所帮助,您将其标记为答案。
关于jquery - 使用 jQuery Mobile 进行 XML 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15994078/