全局Javascript对象如何保存状态? /************************************************************************** * *功能:toggleVis * *描述:以下函数隐藏并展开主列。 * * ***************************************************************************/ //将默认的“显示”模式设置为W3CDOM指定的模式 //兼容浏览器 var showMode='表格单元格'; //然而,IE5至少不能正确地呈现表格单元格 //使用样式“表格单元格”,但在样式“块”时使用 //使用了,所以处理这个 如果(document.all)showMode='block'; //这是实际执行操作的函数 变量状态={}; 功能切换(col){ 如果(!States[col]| | States[col].IsOpen==null) { States[col]={isOpen:true};//这假设单元格已经显示 //States[col]={isOpen:false};//这假设单元格已隐藏 } //模式=状态[col].IsOpen?显示模式:“无”; mode=状态[col].IsOpen?'none':showMode;//从关闭开始,下一步单击需要打开 单元格=document.getElementsByName(col); 对于(j=0;j

全局Javascript对象如何保存状态? /************************************************************************** * *功能:toggleVis * *描述:以下函数隐藏并展开主列。 * * ***************************************************************************/ //将默认的“显示”模式设置为W3CDOM指定的模式 //兼容浏览器 var showMode='表格单元格'; //然而,IE5至少不能正确地呈现表格单元格 //使用样式“表格单元格”,但在样式“块”时使用 //使用了,所以处理这个 如果(document.all)showMode='block'; //这是实际执行操作的函数 变量状态={}; 功能切换(col){ 如果(!States[col]| | States[col].IsOpen==null) { States[col]={isOpen:true};//这假设单元格已经显示 //States[col]={isOpen:false};//这假设单元格已隐藏 } //模式=状态[col].IsOpen?显示模式:“无”; mode=状态[col].IsOpen?'none':showMode;//从关闭开始,下一步单击需要打开 单元格=document.getElementsByName(col); 对于(j=0;j,javascript,html,dom,Javascript,Html,Dom,此函数用于隐藏和显示html表的列。 当我调用此函数时,对象状态会相应地切换,如果展开,则为true,如果隐藏,则为false或none。函数执行一次后,如何保存状态的最后状态,以便在再次调用时可在此函数中使用?是因为对象状态{}被声明为全局状态吗 绝对正确。状态在全局名称空间中声明,可用于所有javascript函数(不使用相同名称的变量隐藏它)。它将在任何使用它的函数之外保留它的值。绝对正确。状态在全局名称空间中声明,可用于所有javascript函数(不使用相同名称的变量隐藏它)。它将在使

此函数用于隐藏和显示html表的列。
当我调用此函数时,对象状态会相应地切换,如果展开,则为true,如果隐藏,则为false或none。函数执行一次后,如何保存状态的最后状态,以便在再次调用时可在此函数中使用?是因为对象状态{}被声明为全局状态吗

绝对正确。状态在全局名称空间中声明,可用于所有javascript函数(不使用相同名称的变量隐藏它)。它将在任何使用它的函数之外保留它的值。

绝对正确。状态在全局名称空间中声明,可用于所有javascript函数(不使用相同名称的变量隐藏它)。它将在使用它的任何函数之外保留它的值。

javascript中的全局变量在刷新或卸载页面之前处于活动状态。

javascript中的全局变量在刷新或卸载页面之前处于活动状态。

是。在最外层的闭包中定义
状态
,这意味着它实际上也是
窗口
对象的属性,即
窗口。状态
==
状态
。然而,您是否定义了一个函数,如

/**************************************************************************
 *
 * Function:    toggleVis
 *
 * Description: Following Function hides and expands the main column.
 *              
 *
***************************************************************************/
// Set the default "show" mode to that specified by W3C DOM
// compliant browsers

  var showMode = 'table-cell';


// However, IE5 at least does not render table cells correctly
// using the style 'table-cell', but does when the style 'block'
// is used, so handle this

  if (document.all) showMode='block';

// This is the function that actually does the manipulation

var States = { };

function toggleVis(col){

    if (!States[col] || States[col].IsOpen == null)
    {
        States[col] = {isOpen : true}; // This assumes the cell is already shown
        //States[col] = {isOpen : false}; // This assumes the cell is already hidden
    } 

    //mode =  States[col].IsOpen ? showMode : 'none';
    mode =  States[col].IsOpen ? 'none' : showMode; //starts from closed, next click need open

    cells = document.getElementsByName(col);
    for(j = 0; j < cells.length; j++) cells[j].style.display = mode;

    States[col].IsOpen = !States[col].IsOpen;
}

它不会影响全局States变量,因为您正在将其重新定义为该函数的局部变量。(但您也可以使用该函数中的
window.States
访问全局状态变量。)

是。在最外层的闭包中定义
状态
,这意味着它实际上也是
窗口
对象的属性,即
窗口。状态
==
状态
。然而,您是否定义了一个函数,如

/**************************************************************************
 *
 * Function:    toggleVis
 *
 * Description: Following Function hides and expands the main column.
 *              
 *
***************************************************************************/
// Set the default "show" mode to that specified by W3C DOM
// compliant browsers

  var showMode = 'table-cell';


// However, IE5 at least does not render table cells correctly
// using the style 'table-cell', but does when the style 'block'
// is used, so handle this

  if (document.all) showMode='block';

// This is the function that actually does the manipulation

var States = { };

function toggleVis(col){

    if (!States[col] || States[col].IsOpen == null)
    {
        States[col] = {isOpen : true}; // This assumes the cell is already shown
        //States[col] = {isOpen : false}; // This assumes the cell is already hidden
    } 

    //mode =  States[col].IsOpen ? showMode : 'none';
    mode =  States[col].IsOpen ? 'none' : showMode; //starts from closed, next click need open

    cells = document.getElementsByName(col);
    for(j = 0; j < cells.length; j++) cells[j].style.display = mode;

    States[col].IsOpen = !States[col].IsOpen;
}
它不会影响全局States变量,因为您正在将其重新定义为该函数的局部变量。(但您也可以通过在该函数中使用
窗口来访问全局状态变量。状态

是的,它们是活动的(直到刷新); 这是我的问题;我将所有弹出窗口(即未保存的输入表单)保存在全局数组中,但刷新后,我无法检索我的弹出窗口,因为我的弹出窗口数组的内容为空;浏览器中是否有可以在浏览器范围内存储数据结构的区域

是,它们是(在刷新之前处于活动状态);
这是我的问题;我将所有弹出窗口(即未保存的输入表单)保存在全局数组中,但刷新后,我无法检索我的弹出窗口,因为我的弹出窗口数组的内容为空;浏览器中是否有可以在浏览器范围内存储数据结构的区域

因此,如果在函数中对其进行操作,但不影响全局,那么当在函数“States[col].IsOpen=!States[col].IsOpen;”的末尾切换状态时,它如何知道下次调用时它在函数的顶部是什么?在您的例子中,它确实影响全局,由于不在函数内重新定义状态。因此,如果在函数中对其进行操作,但不影响全局,则在函数末尾切换状态时,“States[col].IsOpen=!States[col].IsOpen;”它如何知道下次调用函数时它在函数顶部是什么?在您的情况下,它确实会影响全局,因为你没有在函数中重新定义状态。Cookie是我最终使用的。Cookie是我最终使用的