最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - $(this).getAttribute is not a function when using $.each XML Javascript - Stack Overflow

programmeradmin5浏览0评论

I am looping through an XML document and trying to get the name attribute for each one, however Chrome keeps telling me that $(this).getAttribute("name"); is not a function. Anyone have any idea what's going on?

I saw a similar post on this, but it was for passing $(this) in an onclick attribute in a button, not for iterating. Was hoping I could be enlightened on the matter.

XML:

<?xml version="1.0" encoding="UTF-8"?>
<user>
<hotel_group name="Pueblo Bonita" id="1">
    <hotel>
        <name>Pueblo Bonita 1</name>
        <location>
            <address>123 Deer St.</address>
            <city>Montego Bay</city>
            <state>St. Ann's Parish</state>
            <country>Jamaica</country>
        </location>
        <hotel_code>556</hotel_code>
    </hotel>
    <hotel>
        <name>Pueblo Bonita 2</name>
        <location>
            <address>123 Caribou Dr.</address>
            <city>Negril</city>
            <state>Spanish Town</state>
            <country>Jamaica</country>
        </location>
        <hotel_code>555</hotel_code>
    </hotel>
    <hotel>
        <name>Pueblo Bonita 3</name>
        <location>
            <address>30 Milsborough Dr</address>
            <city>Kingston</city>
            <state>Kingston</state>
            <country>Jamaica</country>
        </location>
        <hotel_code>552</hotel_code>
    </hotel>
</hotel_group>
</user>

JS:

            $(xml).find("hotel_group").each(function (k, v) {
                if (!hotelGroups) {
                    hotelGroups = [];
                }

                hotelGroups[k] = $(this).getAttribute("name");

            });

I am looping through an XML document and trying to get the name attribute for each one, however Chrome keeps telling me that $(this).getAttribute("name"); is not a function. Anyone have any idea what's going on?

I saw a similar post on this, but it was for passing $(this) in an onclick attribute in a button, not for iterating. Was hoping I could be enlightened on the matter.

XML:

<?xml version="1.0" encoding="UTF-8"?>
<user>
<hotel_group name="Pueblo Bonita" id="1">
    <hotel>
        <name>Pueblo Bonita 1</name>
        <location>
            <address>123 Deer St.</address>
            <city>Montego Bay</city>
            <state>St. Ann's Parish</state>
            <country>Jamaica</country>
        </location>
        <hotel_code>556</hotel_code>
    </hotel>
    <hotel>
        <name>Pueblo Bonita 2</name>
        <location>
            <address>123 Caribou Dr.</address>
            <city>Negril</city>
            <state>Spanish Town</state>
            <country>Jamaica</country>
        </location>
        <hotel_code>555</hotel_code>
    </hotel>
    <hotel>
        <name>Pueblo Bonita 3</name>
        <location>
            <address>30 Milsborough Dr</address>
            <city>Kingston</city>
            <state>Kingston</state>
            <country>Jamaica</country>
        </location>
        <hotel_code>552</hotel_code>
    </hotel>
</hotel_group>
</user>

JS:

            $(xml).find("hotel_group").each(function (k, v) {
                if (!hotelGroups) {
                    hotelGroups = [];
                }

                hotelGroups[k] = $(this).getAttribute("name");

            });
Share Improve this question edited Nov 25, 2014 at 17:16 haim770 49.2k7 gold badges111 silver badges137 bronze badges asked Nov 25, 2014 at 17:11 Graham S.Graham S. 1632 silver badges10 bronze badges 2
  • 2 getAttribute is a native method, you probably need .attr(). – Teemu Commented Nov 25, 2014 at 17:16
  • hotelGroups[k] = this.getAttribute("name"); will do. – haim770 Commented Nov 25, 2014 at 17:18
Add a ment  | 

1 Answer 1

Reset to default 6

I think you mean:

hotelGroups[k] = $(this).attr("name");

I don't believe that getAttribute is a valid jQuery method.

发布评论

评论列表(0)

  1. 暂无评论