0
<div class="news"><a href="subsides/news.php">
              <p id="p1">Text comes here</p>
              <p id="p2">Another text comes here</p>
              <p id="p3">...</p>
              <p id="p4">...</p></a></div>

如上所述,我有一个带有四个 p 标签的 div,我想从 xml 加载文本!我试过了,在这里搜索,但找到了适合我情况的第 n 个!xml文件和javascript看起来如何?

<?xml version="1.0" encoding="utf-8"?>
  <news name = "one">
              <p id="p1">bla bla bla</p>
              <p id="p2">bla bla bla</p>`
</news>

和javascript:

$.ajax({
    url: "news.xml",
type: "GET",

    dataType: "xml",
    success: function (xml) {
        var xmlDoc = $.parseXML(xml),
            $xml = $(xmlDoc);
        $xml.find('news[name="one"]').each(function () {
            $(".news").append($(this).text());
        });
    }
});
4

2 回答 2

0

从中创建一个html <p>元素,xml <p>然后将其附加到您的 div 中。

$xml.find('news[name="one"] p').each(function () {
    var $p = $("<p>").html($(this).text());
    var id = $(this).attr("id");
    $p.attr("id", id);
    $(".news").append($p);
});

显然,两者<p>并不相同,浏览器会呈现不同的效果。

jsFiddle http://jsfiddle.net/exReT/

于 2013-07-15T22:09:25.403 回答
0

You could use jQuery Ajax to get the XML data... If successful parse the result and search through it to find the node that contains the text you want. If you find the node, append its text to the appropriate paragraph

$.ajax({
    type: "GET",
    url: "docName.xml",
    dataType: "xml",
    success: function (result) {

        var xml = $.parseXML(result),
            $xml = $(xml);

        // Find the node that you want to add to your first paragraph
        // Depending on your situation, you may have to repeat for each paragraph

        $xml.find('xml node you want').each( function() {
            $('#p1').append( $(this).text() );
        });
    }
});
于 2013-07-15T21:53:29.757 回答