jQuery外部XML文件解析 - 无数据检索

时间:2014-03-27 14:53:30

标签: jquery xml

我必须使用xml解析jQuery rss文件,并根据该解析颜色设置背景颜色。但是没有数据检索。

XML Rss文件:colorsxml.xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <channel name = "mycolors">
        <color1>RED</color1>
        <color2>BLUE</color2>
    </channel>
</root>

HTML代码:

<html>
    <head>
        <meta charset="UTF-8">
        <title>Color Replace</title>        
        <style type="text/css">
            body{
                margin: 0;
                padding: 0;
            }
            .box{
                float: left;
                display: block;
                width: 50%;
                height: 600px;                
            }  
        </style>
    </head>
    <body>            
        <div class="box box1">d</div>
        <div class="box box2">d</div>
        <script src="jquery-1.9.0.min.js"></script>
        <script>
            $(document).ready(function(){
            $.ajax({
                type: "GET",
                url: "colorsxml.xml",
                dataType: "xml",
                success: function(xml) {
                    // Parse the xml file and get data
                    var xmlDoc = $.parseXML(xml);
                            $xml = $(xmlDoc);
                    $color1 = $xml.find('channel[name="mycolors"] color1');
                    $color2 = $xml.find('channel[name="mycolors"] color2');
                    $(".box1").text($color1.text()+"HELLO"); // only for test
                    $(".box1").css("background-color", $color1.text());
                    $(".box2").css("background-color", $color2.text());
                }
            });
        });
        </script>
    </body>
</html>

0 个答案:

没有答案