在jQuery中应用其他过滤器

时间:2015-09-24 15:06:00

标签: javascript jquery xml



var xml = '<?xml version="1.0" encoding="UTF-8"?>';
xml = xml + '<states>';
xml = xml + '<state name="KS"> ';
xml = xml + '  <city>Overland Park</city>';
xml = xml + '  <city>Mission</city>';
xml = xml + '  <city>Topeka</city>';
xml = xml + '</state>';
xml = xml + '<state name="MO">';
xml = xml + '<city>Kansas City</city>';
xml = xml + '<city>Belton</city>';
xml = xml + '<city>Clayton</city>';
xml = xml + '</state>';
xml = xml + '</states>';

alert(xml);

xmlDoc = $.parseXML(xml);

$xml = $(xmlDoc);

$(xml).find('state[name="MO"]').each(function() {
  var whichState = $(this).attr("name");
  console.log(whichState);
  $('city', this).each(function() {
    whichCity = $(this).text();
    console.log("City: " + whichCity);
  });

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

我正在尝试按州和城市过滤XML。有没有办法在此语句中应用其他过滤器:$('city', this).each(function(){。 ?

谢谢,

0 个答案:

没有答案