0

私は素朴な jQuery プログラマーなので、誰かこの問題を解決してください。最初の例は、私がやっていることとその動作を示しています。しかし、私のジレンマは、リストが XML を解析して作成されることです。もしそうなら、どのようにxmlを解析し、「タイトル」属性を見つけて、対応するURLをDivにロードしますか? 前もって感謝します...

jQuery コード

 $('.treeLinks').click(function() {
    var sourceURL = $(this).attr('title');
    $('#content').load(sourceURL);
    });

対応する HTML コード

<ul>
<li><a href="#" title="contentArea1.html" class="treeLinks">Link 1</a></li>
<li><a href="#" title="contentArea2.html" class="treeLinks">Link 2</a></li>
</ul>

title 属性を取得するために解析する必要がある XML コード

<?xml version="1.0" encoding="UTF-8"?>
<root>
<item id="pxml_1">
  <content><name class="treeLinks"><![CDATA[Root node 1]]></name></content>
 <item id="pxml_2">
 <content><name class="treeLinks"><![CDATA[Child node 1a]]></name>
 <item id="pxml_23">
 <content><name><![CDATA[Child node 1a]]></name></content>
 </item>
 </content>
 </item>
 <item id="pxml_3">
 <content><name><![CDATA[Child node 2b]]></name></content>
 </item>
 <item id="pxml_4">
 <content><name><![CDATA[Child node 3c]]></name></content>
 </item>
</item>
</root>
4

1 に答える 1

0

HTML と同じ方法で XML を解析します (特別なことは何もありません)。これは Firefox で動作します (なぜ IE が気に入らなかったのかは不明です)。例として .html ファイルとして保存します。

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
        //Document Ready: Everything inside this function fires after the page is loaded
        $(document).ready(function () {
            var test = '<?xml version="1.0" encoding="UTF-8"?><root><item id="pxml_1"><content><name class="treeLinks"><![CDATA[Root node 1]]></name></content> <item id="pxml_2"> <content><name class="treeLinks"><![CDATA[Child node 1a]]></name> <item id="pxml_23"> <content><name><![CDATA[Child node 1a]]></name></content> </item> </content> </item> <item id="pxml_3"> <content><name><![CDATA[Child node 2b]]></name></content> </item> <item id="pxml_4"> <content><name><![CDATA[Child node 3c]]></name></content> </item></item></root>';

            //Need to wrap you xml with a root node
            test = "<wrapper>" + test + "</wrapper>";

            $(test).find('.treeLinks').each(function(){
                alert($(this).html());
            });
        });
    </script>
</head>
<body>
</body>
</html>

あなたの質問の残りの部分は本当に明確ではありませんでした。あなたの xml が上記のリンクをどのように作成しているかわかりませんでした。可能であれば、XML とリンクの関係を明確にしてください。

これがあなたの始まりになることを願っています!

于 2010-09-20T18:29:53.350 に答える