开发者

jQuery XML duplicate append

I'm new to XML. When I try put in some "cases" into it's XML-parent (called ITEM), it fills into all the ITEMS, and dublicates on some. I have 6 cases, but somehow it shows 12 in one of the ITEMs.

I have an XML-file that looks like this:

<menu>
<item>
    <headline />
    <body />
    <cases>
        <case><link /></case>
        <case><link /></case>
        <case><link /></case>
        <case><link /></case>
        <case><link /></case>
        <case><link /></case>
        <case><link /></case>
    </cases>
</item>
<item>
    <headline />
    <body />
</item>

On the ITEM that hasn't any cases in it, I don't want any cases to be shown. I only want the cases to be "put into" the right ITEM.

How do I do this?

To display the items, I use:

$(xml).find('item').each(function(){
    var headline = $("headline", this).text();
    $("ul").append("<li class='" + name + "'><h3>" + headline + "</h3>");

    if ($(xml).find('case').length != 0) {
        //I want it to place this, in the correct ITEM, and not in all items.
        $("ul li").append("<ul class='cases'>");
        $(xml).find('case').each(function(){
            case开发者_如何学编程Link = $("link", this).text();
            $("ul li .cases").append("<li><a href='" + caseLink + "' /></a></li>");

        });
        $("ul li").append("</ul>");
    }
    $("ul").append("</li>");
});

What am I doing wrong???

Thank you in advance...


thoses line :

if ($(xml).find('case').length != 0) {
[...]
$(xml).find('case').each(function(){

should be

if ((this).find('case').length != 0) {
[...]
(this).find('case').each(function(){

or something similar (I did not test it).

Right now, you search inside xml, loop on the item element and then, search case element... in the xml instead of the current item element.


The problem was, that I called the (XML) twice.

The second (and the following) time the "if (XML)" is called, use "(this)" instead.

Change:

if ($(xml).find

To:

if ($(this).find

That did it for me.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜