【问题标题】:Returning XML data parsed via jQuery返回通过 jQuery 解析的 XML 数据
【发布时间】:2012-04-16 15:43:50
【问题描述】:

我正在做的是读取一个 XML 文件并通过 jQuery 将数据获取到我的 HTML 文件中。

正如我在许多教程中发现的那样,我使用的是 jQuery 的 .get() 方法。除了一个问题,它对我很有帮助!

这是 XML 文件:

<?xml version="1.0" encoding="utf-8" ?>
<books>
  <book title="CSS Mastery" imageurl="images/css.jpg">
    <description>
      info goes here.
    </description>
  </book>

  <book title="Professional ASP.NET" imageurl="images/asp.jpg">
    <description>
      info goes here.
    </description>
  </book>

  <book title="Learning jQuery" imageurl="images/lj.jpg">
    <description>
      info goes here.
    </description>
  </book>
</books>

这是我的带有 jQ​​uery 代码的 HTML 文件:

<!doctype html>
<html>
  <head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" >
      $("document").ready(function() {
        $.get("one.xml", function(d) {
          var title = [];
          var description = [];

          $(d).find("book").each(function() {
            title.push($(this).attr("title"));
            description.push($(this).find("description").text());
          });

          console.log(title);
        });
      });
    </script>
  </head>
  <body>
    // BODY contents...
  </body>
</html>

我想要做的是返回标题和描述,以便我可以在其他函数中使用这些数组。根据jQuery代码,我的title数组现在正在console中打印,但是当我尝试在.get()方法之外打印title数组时,它会显示"title is undefined"

我尝试在函数末尾返回数组,但没有成功。我不确定我的问题是否清楚,所以我粘贴了下面给出错误的代码:

<!doctype html>
<html>
  <head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
      $("document").ready(function() {
        $.get("one.xml", function(d){
          var title = [];
          var description = [];

          $(d).find("book").each(function() {
            title.push($(this).attr("title"));
            description.push($(this).find("description").text());
          });
        });

        console.log(title);
      });
    </script>
  </head>
  <body>
    // BODY contents...
  </body>
</html> 

在这段代码中,它是 "title isn't defined,注意我在 .get() 方法之外对 title 数组进行了调整。

【问题讨论】:

    标签: javascript xml jquery xml-parsing scope


    【解决方案1】:

    只需像下面这样使用:

    title = [];
    description = [];
    

    如果你不在变量名前使用var,它将在全局范围内,所以你也可以在其他函数中使用这两个变量。

    【讨论】:

    • @Tonu Bhai,它会起作用,但这是一个非常糟糕的做法:(顺便说一句,console.log 仍然不起作用,因为它会在回调将数据推送到该数组之前执行。跨度>
    • 可以根据情况定义坏或好的做法:) 在这种情况下,他必须在该范围内使用回调方法。
    • 他再次可以使用 jQuery.deferred 来使用带有 .get() 回调的附加回调。
    • 我尝试过声明没有 var 关键字的变量,但没有任何改变。我没有不耐烦之类的,但我忍不住问“所以没有办法从那个函数中取出那个变量?”
    【解决方案2】:

    因为变量作用域,看看here。如果您想在 javascript 的任何其他函数中使用标题和描述,那么您可以在保存标题和描述($.get 的回调函数内部)后立即直接调用该 javascript 函数,如下所示。

    $("document").ready(function(){
    $.get("one.xml", function(d){
        var title       = [];
        var description = [];
        $(d).find("book").each(function(){
            title.push($(this).attr("title"));
            description.push($(this).find("description").text());
        });
        call_to_your_function_with_title_and_desc(title, description)
    });
    

    【讨论】:

    • 我以前也想过这个,但我只是好奇是否还有其他方法。无论如何,我对 jquery 还是很陌生,感谢您的帮助!竖起大拇指!
    • @FoysalAhmed - 我理解你学习 jQuery 的好奇心,但我总是更喜欢你搜索已经问过的问题,如果你没有找到问题的答案然后创建新问题......无论如何祝你好运。 ..!
    • 我已经通过 google 和 stackoverflow 进行了搜索,但我没有找到任何人以这种方式使用它。我所看到的大部分教程和解决方案都是关于从文档中的 xml 文件打印接收到的数据。这就是为什么我不得不发布这个问题。我认为可能有一些方法可以在函数之外返回数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    相关资源
    最近更新 更多