开发者

jquery class selector doesn't work in IE6

Eidt: Thanks to all and sorry for my carelessness. The initTree() fails.

This works fine in FF and Chrome:

$("#tree").treeview({
    collapse:false,
});

This works fine in FF Chrome and IE6:

$("#tree").treeview({
    collapse:false  //<-here is the key, no comma
});

I have a tree like:

<ul id="tree">
    <li>Root
        <ul>
            <li>Node_1_2<a class='addnode'>add</a><a class='deletenode'>delete</a>
                <ul>
                    <li>Node_2_4<a class='addnode'>add</a><a class='deletenode'>delete</a></li>
                    <li>Node_2_6</li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

If I click the <a>add</a>, it should append a chid node to that element. Something like:

$("a.addnode").live("click", function() {
    if($(this).parent().children("ul").html() == null){
        leafHtml = ...;
        $(this).parent().append(leafHtml);
    }
    else{
开发者_开发技巧        leafHtml = ...;
        $(this).parent().children("ul").append(leafHtml);
    }
    initTree();
});

This works fine in Firefox and Chrome. But in IE6 it did nothing. Seem like $("a.addnode") didn't work.


First of all you need some way to debug what is happening. There are things like Firebug lite you can check out but I would suggest just placing a couple of alerts.

$("a.addnode").live("click", function() {
    alert('click caught');
    if($(this).parent().children("ul").html() == null){
        alert('no children');
        leafHtml = ...;
        $(this).parent().append(leafHtml);
    }
    else{
        alert('children');
        leafHtml = ...;
        $(this).parent().children("ul").append(leafHtml);
    }
    alert('end');
    initTree();
});

You will know what get's called and what doesn't get called instead of guessing. If I should take a guess there are two errors here that cause this problem. Your selector is fine.

1: $(this).parent().children("ul").html() == null will never be true. You should check against $(this).parent().children("ul").children().length == 0 instead.

2: $(this).parent().children("ul").append(leafHtml) makes no sense. Text inside an ul is not valid and I wouldn't be suprised if IE6 just ignores it(it should).

I suspect that #1 makes #2 always happend and as that is invalid IE6 just ignores it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜