简体   繁体   中英

How can I get the attribute value of a XML element using JQuery?

I'm want to get the value of an attribute using JQuery. This is a part of my XML:

<VertrekkendeTrein>
    <RitNummer>1583</RitNummer>
    <VertrekTijd>2014-09-09T21:24:00+0200</VertrekTijd>


    <EindBestemming>Amersfoort</EindBestemming>
    <TreinSoort>Intercity</TreinSoort>

        <RouteTekst>Hoorn, A'dam Sloterdijk, Amsterdam C.</RouteTekst>

        <Vervoerder>NS</Vervoerder>

    <VertrekSpoor wijziging="false">1</VertrekSpoor>

        <ReisTip>Stopt tot Hoorn op tussengelegen stations</ReisTip>

</VertrekkendeTrein>

I want to know if wijziging is "true" of "false". I tried this using JQuery:

var tijden = $(response).find("VertrekSpoor").attr("wijziging");

for (var i = 0; i < tijden.length; i++) {
    if (tijden === "true") {
        $(".spoor").css({'color':'rgba(255,0,4,1.00)'});    
    }   
}

Hopefully someone can help me with this. I know there are more questions about this problem, but I couldn't find one that fixed my problem.

you are doing it right. I think either your XML is breaking because of Quotation marks "" or before writing your find method you have not parsed it.
Just tried with your script and it is working. Hope this will help you :)

           <script type="text/javascript">
        $(document).ready(function () {
            var yourxml = "<VertrekkendeTrein><RitNummer>1583</RitNummer><VertrekTijd>2014-09-09T21:24:00+0200</VertrekTijd>" +
                        "<EindBestemming>Amersfoort</EindBestemming><TreinSoort>Intercity</TreinSoort><RouteTekst>" +
                        "Hoorn, A'dam Sloterdijk, Amsterdam C.</RouteTekst><Vervoerder>NS</Vervoerder><VertrekSpoor wijziging='false'>1" +
                    "</VertrekSpoor><ReisTip>Stopt tot Hoorn op tussengelegen stations</ReisTip></VertrekkendeTrein>";
            xmlDoc = $.parseXML(yourxml);
            $xml = $(xmlDoc);
            tijden = $xml.find("VertrekSpoor").attr("wijziging");
            if (tijden === "true") {
                $(".spoor").css({ 'color': 'rgba(255,0,4,1.00)' });
            }
        });
    </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM