I am parsing an xml which looks like this

<title>abc</title>
<summary>abc</summary>
<content type='application/atom+xml' src='abc'/>
<link rel='alternate' type='application/atom+xml' href='abc'/>
<title>abc</title>
<summary>xyz</summary>
<content type='application/atom+xml' src='xyz'/>
<link rel='alternate' type='application/atom+xml' href='xyz'/>
<title>abc</title>
<summary>abb</summary>
<content type='application/atom+xml' src='abb'/>
<link rel='alternate' type='application/atom+xml' href='abb'/>

My jQuery:

$title.each(function(index)
  {
    if (index != 0) 
        {
          $("#container").append('<div id=' + index + '></div></br>');
      $('#' + index).text($(this).text());
      $srcnode = $(xml).find('content')[index];
      alert($srcnode.attr('src'));
    }
}

I am getting the error as no attr 'src' found for the element. I am trying to fetch the link corresponding to the title which is in content

有帮助吗?

解决方案

try to change

  $srcnode = $(xml).find('content')[index];

to

  $srcnode = $(xml).find('content').eq(index);

(+ you have no "xml" variable.) After you correct it, it should work correctly then

其他提示

Change

$srcnode = $(xml).find('content')[index];

To

$srcnode = $(xml).find('content').eq(index);

attr is a jQuery method, when you $(xml).find('content')[index] it will give you the xml node. jQuery has eq method which takes an integer as parameter and returns the element at that index from the matched sets of elements.

In addition to [] giving you a DOM Node rather than a jQuery wrapper, you have two further issues:

$(xml)

$() is a shortcut to parse HTML and create nodes from it. But your content is not HTML, it's XML, and if you try to parse it as HTML you're going to confuse the browser (especially if it's IE). Use $.parseXML() to parse XML.

'<div id=' + index + '></div></br>'

Avoid purely-numeric ids, they are not valid and can confuse browsers. You don't need to look up an id anyway, you can just use the reference to the node you've already got, eg $('<div/>', {text: $(this).text()}).appendTo('#container');.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top