$(xml).find("yweather:location").each(...); // 選擇器中冒號被視為偽類
$(xml).find("yweather\\:location").each(function() {
var city = $(this).attr("city");
$("#show2").text(city);
});
jQuery 選擇器引擎 (Sizzle) 將冒號 : 解析為偽類選擇器(如 :first、:checked)。XML 命名空間前綴 yweather: 中的冒號需用 \\: 轉義(字符串中寫兩個反斜杠,實際傳給引擎為 \:)。
$.ajax({
url: "https://xml.weather.yahoo.com/forecastrss?p=CHXX0008&u=f",
dataType: "xml",
success: function(xml) {
$(xml).find("yweather\\:location").each(function() {
$("#show2").text($(this).attr("city"));
});
}
});
getElementsByTagNameNS:xml.getElementsByTagNameNS("https://xml.weather.yahoo.com/ns/rss/1.0", "location")。