老式javascript在Chrome和IE中不起作用

老式javascript在Chrome和IE中不起作用,javascript,internet-explorer,firefox,google-chrome,Javascript,Internet Explorer,Firefox,Google Chrome,我需要解决我们仍然使用的旧系统上的一个问题。我正忙于将一个新系统转换为jquery,但这些来自旧系统的金块有时会弹出 下面的代码在Firefox中运行良好,但在Chrome或IE中根本不起作用 我添加了一个console.log(“test”),它可以在Firefox中运行,但不能在Chrome或IE中运行 请让我知道在vanilla javascript中实现这一点的最佳方法是什么,这样跨浏览器友好。谢谢 代码 这应该适用于所有浏览器 function showlaptop(theTable)

我需要解决我们仍然使用的旧系统上的一个问题。我正忙于将一个新系统转换为jquery,但这些来自旧系统的金块有时会弹出

下面的代码在Firefox中运行良好,但在Chrome或IE中根本不起作用

我添加了一个console.log(“test”),它可以在Firefox中运行,但不能在Chrome或IE中运行

请让我知道在vanilla javascript中实现这一点的最佳方法是什么,这样跨浏览器友好。谢谢

代码


这应该适用于所有浏览器

function showlaptop(theTable)
{
  if (document.getElementById(theTable).style.display == 'none')
  {
        document.getElementById(theTable).style.display = '';
        console.log("test");
  }
}

function hidelaptop(theTable)
{
  if (document.getElementById(theTable).style.display == '')
  {
        document.getElementById(theTable).style.display = 'none';
  }
  else
  {
        document.getElementById(theTable).style.display = 'none';
  }
}
也就是说,将
'block'
替换为
'

你不需要第二个,顺便说一句

更新:


正如Rob在评论中提到的。所有内容的默认显示值都不是
'block'
,尽管有些浏览器允许,但遵循标准的浏览器不会相应地呈现页面…

错误似乎不在您提供的代码段中…您遇到了什么错误?可能正在为表传递一个值,该值在页面上不作为id存在。如果在运行代码之前打开控制台,Chrome是否会告诉您错误?(Ctrl-shift-J)另外,
hidelaptop()
函数在其if和else分支中也在做同样的事情。是否有任何特殊原因明确地将
display
设置为
block
会失败?我似乎记得这是默认值,但绝对不是直接声明它的问题。因为table元素的默认值是
table
,而不是
block
。但是,有些浏览器不支持标准值,也不会显示任何错误,但是那些坚持标准的浏览器会显示错误。
function showlaptop(theTable)
{
  if (document.getElementById(theTable).style.display == 'none')
  {
        document.getElementById(theTable).style.display = '';
        console.log("test");
  }
}

function hidelaptop(theTable)
{
  if (document.getElementById(theTable).style.display == '')
  {
        document.getElementById(theTable).style.display = 'none';
  }
  else
  {
        document.getElementById(theTable).style.display = 'none';
  }
}