$(this).getAttribute 在使用 $.each XML Javascript 时不是一个函数

$(this).getAttribute is not a function when using $.each XML Javascript

本文关键字:一个 函数 each getAttribute this XML Javascript      更新时间:2023-09-26

我正在循环访问一个XML文档并试图获取每个文档的name属性,但是Chrome一直告诉我$(this).getAttribute("name");不是一个函数。有人知道发生了什么吗?

我看到了一篇类似的帖子,但它是为了在按钮中的onclick属性中传递$(this),而不是用于迭代。希望我能对此事有所启发。

.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");
            });

我想你的意思是:

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

我不认为getAttribute是一个有效的jQuery方法。