Jquery 属性选择
我正在解析一个类似于这样的 xml
<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'));
}
}
我收到错误,因为找不到该元素的 attr 'src' 。我正在尝试获取与内容中的标题相对应的链接
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试更改
为
(+ 你没有“xml”变量。) 更正后,
那么它应该可以正常工作
try to change
to
(+ you have no "xml" variable.) After you correct it,
it should work correctly then
Change
To
attr
是一个 jQuery 方法,当您$(xml).find('content')[index]
时,它会给您 xml 节点。 jQuery 有eq
方法,它接受一个整数作为参数,并从匹配的元素集中返回该索引处的元素。Change
To
attr
is a jQuery method, when you$(xml).find('content')[index]
it will give you the xml node. jQuery haseq
method which takes an integer as parameter and returns the element at that index from the matched sets of elements.除了
[]
为您提供 DOM 节点而不是 jQuery 包装器之外,您还有两个进一步的问题:$()
是解析 HTML 并从中创建节点的快捷方式。但您的内容不是 HTML,而是 XML,如果您尝试将其解析为 HTML,您将会迷惑浏览器(尤其是 IE)。使用$.parseXML()
解析 XML。避免使用纯数字的
id
,它们无效并且可能会迷惑浏览器。无论如何,您不需要查找id
,只需使用对已有节点的引用即可,例如$('
', {文本:$(this).text()}).appendTo('#container');
。In addition to
[]
giving you a DOM Node rather than a jQuery wrapper, you have two further issues:$()
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.Avoid purely-numeric
id
s, they are not valid and can confuse browsers. You don't need to look up anid
anyway, you can just use the reference to the node you've already got, eg$('<div/>', {text: $(this).text()}).appendTo('#container');
.