javascript 函数行为异常

发布于 2024-10-05 00:43:19 字数 2438 浏览 0 评论 0原文

嘿伙计们,我无法让一个功能正常工作,重点是爬过父母,直到我得到一个触发切换动作的人。但是该函数的行为很奇怪,当找到经过验证的标签时,它似乎没有返回 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 */

$(document).ready(function(){

$("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

hey guys i cant get a function working properly, the point is to climb over the pareents till i get the one to trigger the toggle action. But the function acting weirdly and when the validated tag is found it seems to doesnt returning true and complete the recursive way.

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)
});

the whole code :

/* when the page is loaded */

$(document).ready(function(){

$("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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿透光 2024-10-12 00:43:19

e.siblings(...).each 内的函数未从 dock 返回 true

所以

    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

The function inside e.siblings(...).each is not returning true from dock.

So instead of

    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
        }       
    })

I think you need to say something like:

    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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文