开发者

Get name attribute for multiple parents

I have the following code:

开发者_运维问答
var parentEls = $(root)
                .find("credit standard")
                .parents()
                .map(function () { 
                  return this.tagName; 
                    })
                .get()
                .join(" > ");
                alert(parentEls);

            $("#breadcrumb").append("<p>" + parentEls + "</p>");

This works fine to return the parents. However, I don't want to return tagName, I want the name attribute for each parent instead. I tried

.map(function () { 
                      return this.attr("name"); 
                        })

but this doesn't work. Is there any way to do this without resorting to a loop?


attr is no native method, it's a jQuery method. So try to wrap your this into a jQuery constructor:

return $(this).attr('name');

or use the getAttribute method instead:

return this.getAttribute('name');


You have to wrap the element with jQuery to use .attr. Either use

this.name

or $(this).attr('name')


I would try the following

.map(function () { 
   return $(this).attr("name"); 
 })
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜