views:

17

answers:

1

this is my code :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
  <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"&gt;&lt;/script&gt;--&gt;
    <script type="text/javascript" src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
    <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&amp;hl=zh-CN&amp;vps=1&amp;jsv=259e&amp;msa=0&amp;output=georss&amp;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

A: 

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.

Anurag