jQuery每个循环输出
您好,我有一个关于如何使用jQuery在每个循环中显示xml文件中的所有数据的问题,它现在只显示一条记录,谢谢。 html代码:jQuery每个循环输出,jquery,html,each,weather,Jquery,Html,Each,Weather,您好,我有一个关于如何使用jQuery在每个循环中显示xml文件中的所有数据的问题,它现在只显示一条记录,谢谢。 html代码: <label id="lbCity">City:<p id="city"></p></label> <label id="lbDate"><p id="date"></p></label> <label id="lbRain">Rain:<p id="ra
<label id="lbCity">City:<p id="city"></p></label>
<label id="lbDate"><p id="date"></p></label>
<label id="lbRain">Rain:<p id="rain"></p></label>
<label id="lbWind">Wind:<p id="wind"></p></label>
<label id="lbMin">Min:<p id="min"></p></label>
<label id="lbMax">Max:<p id="max"></p></label>
<label id="lbHumi">Humidity:<p id="humi"></p></label>
<label id="lbCloud">Cloud:<p id="description"></p></label>
问题在于,每次使用
html()
设置标签的值时,尝试用append()
替换标签,您应该会看到标签中的所有值。如果要将它们分开,请使用js为每一行重新创建html结构,然后将其附加到容器中。请使用$(this).find()而不是xml.find()
$(document).ready(function(e) {
$("#Button").on('click', function(){
loading(true);
$.ajax({
type:"GET",
url: url,
dataType:'text', // type of the file format
success: function(data){
var xmlDoc = $.parseXML(data);
var xml = $(xmlDoc);
$(xml).find('forecast time').each(function(){
$.each(this.attributes, function(i, attrib){
console.log( $($.parseXML(data)).find("country").text() );
$("#city").html( xml.find("name") );
$("#date").html( xml.find("time").attr("day") );
$("#min").html( xml.find("temperature").attr("min") + "°C" );
$("#max").html( xml.find("temperature").attr("max") + "°C" );
});
});
},
});
});