【问题标题】:How to read an xml file using JQuery?如何使用 JQuery 读取 xml 文件?
【发布时间】:2016-01-09 07:46:51
【问题描述】:

我目前正在从本地 xml 文件中获取数据。但是我的代码不起作用。我尝试了很多解决方案,但没有一个不适合我。 “data.xml”文件与“index.html”在同一个文件夹中。我正在使用 Chrome 进行测试。

数据.xml

<?xml version="1.0" encoding="utf-8"?>
<Names>
  <Name id="10" />
  <Name id="11" />
</Names>

index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
   <script src="Resources/jquery-1.8.3.min.js"></script>
    <script>
        $(document).ready(function () {
            $.ajax({
                type: "GET",
                url: "data.xml",
                dataType: "xml",
                success: function (xml) {
                    $(xml).find('Name').each(function (element) {
                        var sTitle = $(element).attr('id');
                        alert(sTitle);
                    });
                }
            });
        });
    </script>
</head>

【问题讨论】:

标签: jquery xml parsing


【解决方案1】:

只需将var sTitle = $(element).attr('id'); 更改为:

var sTitle = $(this).attr('id');

这应该可以解决您的问题。

我注意到您使用的是旧的和已弃用的 jquery 版本。我建议您使用最新版本。您也可以考虑使用 json 而不是 xml。比较简单。

【讨论】:

    猜你喜欢
    • 2012-11-03
    • 2010-12-21
    • 2011-09-06
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多