开发者

Jquery XML parsing

I have this XML

<items>
  <item validate="NotEmpty ValidEmail" class="xxx yyy zzz" type="input" name="Id" value="" label="Id" />
  <item validate="NotEmpty" type="input" name="NazovProjektu" value="" label="NazovProjektu" />
  <item type="in开发者_运维技巧put" name="Oz" value="" label="Oz" />

I want to get the value of the attribute name of each element with attribute class containing string yyy


This might work, haven't tested it yet. Assuming the XML is already parsed and inside an object named xml.

$(xml).find("[class~='yyy']").each(function(i, element) {
    console.log($(element).attr("name"));
});


I believe you can do something like

$("item[class*=yyy]", xml).each(function() { 
    $(this).attr('name'); 
});


try following, it should help you out.

$(document).ready(function()
{
  $.ajax({
    type: "GET",
    url: "data.xml",
    dataType: "xml",
    success: function(xml)
        {
          //find every yyy classname and alert its name
          $(xml).find(".yyy").each(function()
          {
            alert($(this).attr('name'));
          });
        }
  });
});


A class selector will get you the elements. The attr() function will get you the name of the element. Using each() on the jQuery Object will allow you to iterate through the element in the jQuery Object.

var yourXML = "...";
$("[class*='zzz']", yourXML).each(function() {
   alert($(this).attr("name"));
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜