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");
})
精彩评论