javascript函数很奇怪

时间:2010-11-28 14:40:25

标签: javascript javascript-framework

嘿伙计们,我不能让一个功能正常工作,重点是越过主要人员,直到我得到一个触发切换动作。但是这个函数很奇怪,当找到经过验证的标记时,似乎没有返回true并完成递归方式。

unction dock(e){
    if(e.get(0).tagName != "BODY"){
        alert("current : "+e.get(0).tagName)
        e.siblings(".splitter-bar").each(function(){
            alert("found sibling : "+$(this).get(0).tagName)

            if($(this).length > 0){
                e.parent().trigger('toggleDock')
                alert("triggered parent : "+e.parent().get(0).tagName)
                return true
            }       
        })
        if ( dock(e.parent()) ){
            alert("validated parent : "+e.parent().get(0).tagName)
            return true
        }
        alert("end dock(), current : "+e.get(0).tagName)
    }
    return false 
}

$(".dockme").live('click', function(event) {
    a = dock($(this))
    alert("dock returned :"+a)
});

整个代码:

/* when the page is loaded */

(文档)$。就绪(函数(){

$("body").splitter({
    splitVertical: true,
    outline: true,
    sizeLeft: 30, minLeft: 0, maxLeft: 100,
    resizeToWidth: true,
    anchorToWindow: true,
    dock: "left",
    dockSpeed: 200,
    cookie: "docksplitter"
});
$("#a").splitter({
    splitHorizontal: true,
    outline: true,
    sizeBottom: 30, minBottom: 0, maxBottom: 30,
    dock: "bottom",
    dockSpeed: 200,
    cookie: "docksplitter"
});
$("#b").splitter({
    splitVertical: true,
    sizeRight: 320, minRight: 0, maxRight: 320,
    dock: "right",
    dockSpeed: 200,
    cookie: "docksplitter"
});

function dock(e){
    if(e.get(0).tagName != "BODY"){
        alert("current : "+e.get(0).tagName)
        e.siblings(".splitter-bar").each(function(){
            alert("found sibling : "+$(this).get(0).tagName)

            if($(this).length > 0){
                e.parent().trigger('toggleDock')
                alert("triggered parent : "+e.parent().get(0).tagName)
                return true
            }       
        })
        if ( dock(e.parent()) ){
            alert("validated parent : "+e.parent().get(0).tagName)
            return true
        }
        alert("end dock(), current : "+e.get(0).tagName)
    }
    return false 
}

$(".dockme").live('click', function(event) {
    a = dock($(this))
    alert("dock returned :"+a)
});

}); html:http://pastie.org/1329885

1 个答案:

答案 0 :(得分:0)

e.siblings(...).each内的功能未从true返回dock

所以而不是

    e.siblings(".splitter-bar").each(function(){
        alert("found sibling : "+$(this).get(0).tagName)

        if($(this).length > 0){
            e.parent().trigger('toggleDock')
            alert("triggered parent : "+e.parent().get(0).tagName)
            return true
        }       
    })

我认为你需要说:

    var triggeredInSplitterBar = false;  // ADDED

    e.siblings(".splitter-bar").each(function(){
        alert("found sibling : "+$(this).get(0).tagName);

        if($(this).length > 0){
            e.parent().trigger('toggleDock');
            alert("triggered parent : "+e.parent().get(0).tagName);
            triggeredInSplitterBar = true;  // CHANGED
        }
    });

    if (triggeredInSplitterBar) { return true; }  // ADDED