如何使用 google ajax feed api 在我的代码中获取“guid”的nodeValue

发布于 2024-09-11 21:10:34 字数 1720 浏览 2 评论 0原文

这是我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google AJAX Feed API - Simple Example</title>
    <!--<script type="text/javascript" src="http://www.google.com/jsapi"></script>-->
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript" src='jquery-1.4.2.js'></script>
    <script type="text/javascript">

    google.load("feeds", "1");
    function initialize() {
      var feed = new google.feeds.Feed("http://maps.google.com/maps/ms?ie=UTF8&hl=zh-CN&vps=1&jsv=259e&msa=0&output=georss&msid=109685068115364659392.00048b5b630141d82b83a");

      feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
      feed.load(function(result) {
          if (!result.error) {
            for (var i = 0; i < result.feed.entries.length; i++) {
              var entry = result.feed.entries[i];
             var guid=entry.xmlNode.getElementsByTagName("id") || entry.xmlNode.getElementsByTagName("guid") ||0
            //console.log($('id',entry.xmlNode.xmlDocument)||$('guid',entry.xmlNode.xmlDocument))
            console.log(guid)
            }
          }
        });
  }
    google.setOnLoadCallback(initialize);

    </script>
  </head>
  <body>
    <div id="feed"></div>
  </body>

我的代码什么也没有得到,

我使用jquery的方法text()或html(),但也没有,

所以我能做什么。

谢谢

this is my code :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google AJAX Feed API - Simple Example</title>
    <!--<script type="text/javascript" src="http://www.google.com/jsapi"></script>-->
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript" src='jquery-1.4.2.js'></script>
    <script type="text/javascript">

    google.load("feeds", "1");
    function initialize() {
      var feed = new google.feeds.Feed("http://maps.google.com/maps/ms?ie=UTF8&hl=zh-CN&vps=1&jsv=259e&msa=0&output=georss&msid=109685068115364659392.00048b5b630141d82b83a");

      feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
      feed.load(function(result) {
          if (!result.error) {
            for (var i = 0; i < result.feed.entries.length; i++) {
              var entry = result.feed.entries[i];
             var guid=entry.xmlNode.getElementsByTagName("id") || entry.xmlNode.getElementsByTagName("guid") ||0
            //console.log($('id',entry.xmlNode.xmlDocument)||$('guid',entry.xmlNode.xmlDocument))
            console.log(guid)
            }
          }
        });
  }
    google.setOnLoadCallback(initialize);

    </script>
  </head>
  <body>
    <div id="feed"></div>
  </body>

and my code did not get nothing ,

i use jquery's method text() or html() ,but not too ,

so what can i do .

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷港 2024-09-18 21:10:34

尝试

$(entry.xmlNode).find('guid').text()

$('guid', entry.xmlNode).text()

但是,你不能这样做:

$(something) || $(somethingElse)

因为,代码永远不会到达 $(somethingElse),因为 $(something) 将返回一个 jQuery 对象,即使没有匹配成立。一个对象(甚至是一个空的 jQuery 对象)是 truthy,因此无论 $(somethingElse) 的值是什么,表达式都将为 true,JavaScript 会完全跳过对它的求值。这称为短路评估。如果您确实想先通过 "id" 查找,然后通过 "guid" 查找,那么请尝试:

var xml = entry.xmlNode;
var guid = $('id', xml).text() || $('guid', xml).text() || 0;
console.log(guid);

这样做的原因是因为我们试图在每个中获取一个字符串表达式的步骤,而不是对象。空字符串在 JavaScript 中是假的,因此像这样的表达式

"" || 42; // 42
"" || (40 + 2); // 42

将返回第二个表达式 (42) 作为结果。

Try

$(entry.xmlNode).find('guid').text()

or

$('guid', entry.xmlNode).text()

But, you cannot do:

$(something) || $(somethingElse)

because, the code will never get to $(somethingElse), since $(something) will return a jQuery object even if no match is found. An object (even an empty jQuery object) is truthy, thus the expression is going to be true no matter what the value of $(somethingElse), JavaScript skips evaluating it altogether. It's called short-circuit evaluation. If you do want to find first by "id", then by "guid", then try:

var xml = entry.xmlNode;
var guid = $('id', xml).text() || $('guid', xml).text() || 0;
console.log(guid);

The reason this works is because we are trying to get a string in each step of the expression, and not an object. An empty string is falsy in JavaScript, hence expressions like:

"" || 42; // 42
"" || (40 + 2); // 42

will return the second expression (42) as the result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文