为什么这个节目隐藏javascript导致cms内的问题

时间:2011-12-29 03:38:46

标签: javascript html

代码应该显示或隐藏CMS内部表单上的输入(和其他元素)。但是,某些附件的行为就好像被删除了一样,不会被处理或发送。如果没有我发布几页代码并解释CMS特定代码,您能告诉我哪些代码块可能会导致问题?

这是我将问题缩小到相关的相关代码段。

function setShowHide(num, min)
{
    var terminate = false;

    if (num < 0) // minus
    {
        for(x = 4; x > 0; x--) 
        {
            if(terminate != true)
            {
                if(showHideArray[x] == 1 && AttArray[x] == 0)
                {
                    hide(x + 1);
                    showHideArray[x] = 0;
                    terminate = true;
                }
            }
        }
    }

    if (num > 0) // plus
    {
        for(x = 0; x < 5; x++) 
        {
            if(terminate != true)
            {
                if(showHideArray[x] == 0)
                {
                    Show(x + 1);
                    showHideArray[x] = 1;
                    terminate = true;
                }
            }    
        }
    }
}

function hide(i) {

    changeObjectVisibility("Attachmenttd" + i, "none");
    changeObjectVisibility("Attachment" + i + "File", "none");
    changeObjectVisibility("Attachment" + i + "Description", "none");
    changeObjectVisibility("Attachment" + i + "If", "none");
}           

function Show(j) {                   
    changeObjectVisibility("Attachmenttd" + j, "inline");
    changeObjectVisibility("Attachment" + j + "File", "inline");
    changeObjectVisibility("Attachment" + j + "Description", "inline");
    changeObjectVisibility("Attachment" + j + "If", "inline");

}

function changeObjectVisibility(objectId, newVisibility) {
    // first get the object's stylesheet
    var styleObject = getStyleObject(objectId);
    // then if we find a stylesheet, set its visibility
    // as requested
    //
    if (styleObject) {
        styleObject.display = newVisibility;
        return true;
    } else {
        return false;
    }
}

function getStyleObject(objectId) {
    // checkW3C DOM, then MSIE 4, then NN 4.
    //
    if (document.getElementById(objectId)) {
        return document.getElementById(objectId).style;
    }
    else if (document.all && document.all(objectId)) {
        return document.all(objectId).style;
    }
    else if (document.layers && document.layers[objectId]) {
        return document.layers[objectId];
    } else {
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

我可以看到您的代码存在一些问题(可能会或可能不会解释您抱怨的症状):

你的两个for循环不在相同的数组元素上运行:

for(x = 4; x > 0; x--)

for(x = 0; x < 5; x++)

在两个循环中你有showHideArray[x],但第一个循环倒计时到过程元素4,3,2,1(停止),而第二个循环倒计时到过程元素0,1,2, 3,4(停止)。也就是说,只有第二个循环处理元素0.第一个for循环应该是:

for(x = 4; x >= 0; x--)          //    >= 0 rather than > 0

另请注意,您没有声明x因此它将是全局的 - 如果它们还有未声明的x,则可能会对代码的其他部分产生负面影响。

我认为这不会导致任何问题,但您不需要terminate变量和if(!terminate)内容 - 这就是break keyword的用途。

您永远不会使用min参数。

关于您的getStyleObject()功能,您真的需要支持没有getElementById()的浏览器吗?或者这是否尝试允许提供的参数可能是id或可能是名称?