代码之家  ›  专栏  ›  技术社区  ›  ant

非常不寻常的javascript行为

  •  2
  • ant  · 技术社区  · 15 年前

    这是我用了很久的东西,突然间我有了这些奇怪的行为,也许有人知道为什么?这是我的HTML:

    <div class="tooltip" id="tooltip"></div>
    
    <input type="button" name="hide" value="hide" onclick="hide('tooltip');" />
    <input type="button" name="show" value="show" onclick="show('tooltip');" />
    

    下面是HTML代码下面的javascript:

        <script type="text/javascript">
            function hide(id)
            {
                document.getElementById(id).style.display = 'none';
            }
            function show(id)
            {
                document.getElementById(id).style.display = '';
            }
        </script>
    

    现在这个代码不应该有任何问题。它应该在每次单击按钮时隐藏或显示工具提示。现在奇怪的是,当我点击隐藏按钮时,它会自动隐藏起来,当我点击显示时,什么也不会发生。隐藏仍然隐藏。

    有人有类似的问题吗?有什么工作要做吗?也许是完成同样事情的另一种方法(纯javascript)?

    更新: 已将其更改为block,但仍不起作用。我的意思是,当没有任何连接时,为什么它会隐藏我点击的按钮?顺便说一下,我正在使用最新的火狐。我在函数中添加了alert。以下是重新编写的代码:

    function hide(id)
    {
        alert(id);
        document.getElementById(id).style.display = 'none';
    }
    function show(id)
    {
        alert(id);
        document.getElementById(id).style.display = 'block';
    }
    
    11 回复  |  直到 15 年前
        1
  •  4
  •   Community datashaman    7 年前

    似乎没有任何明显的迹象表明这会导致工作不正常。我猜想有某种类型的错别字、未闭合的标记或函数等。请尝试添加一个 alert() 以查看函数是否被正确调用。

    编辑: 根据您对问题的最新编辑,我倾向于页面上有另一个具有相同ID的项目。请参见 this question .

    还有一件事要尝试:在你的 show 功能,在设置 display 属性,尝试执行 alert 显示 属性以查看其实际设置为:

    alert(document.getElementById(id).style.display);
    
        2
  •  2
  •   Jauco    15 年前

    当我复制粘贴您的代码并将其加载到Google Chrome中时,效果非常好。你可能在原版中有错别字。

        3
  •  1
  •   jbrass    15 年前

    我相信,display=“none”的反面是display=“inline”或display=“block”,这取决于您希望它如何显示。

        4
  •  1
  •   GSto    15 年前

    我看不出您编写的代码有什么特别的错误,但是您可能想尝试在show函数上将display设置为“block”。

        5
  •  1
  •   Pekka    15 年前

    我敢打赌,有两种元素的ID是“工具提示”。

        6
  •  0
  •   Senior Coconut    15 年前

    您需要将显示类型设置回block:

    <script type="text/javascript">
    
                  function hide(id)
                  {
                  document.getElementById(id).style.display = 'none';
                  }
                  function show(id)
                  {
                  document.getElementById(id).style.display = 'block';                
                  }
    
        </script>
    
        7
  •  0
  •   David Hedlund    15 年前

    你可能想切换到“块”,当显示它时,如其他人所说。除此之外,如果默认情况下它是隐藏的,那么它必须被内联样式隐藏。如果用放置在外部CSS文件中的代码隐藏它,将无法用JavaScript再次显示它。

        8
  •  0
  •   Abgan    15 年前
    1. 检查一下 display='block' 事情-也许你的CSS指定了 display='none' 上课 .tooltip ?

    2. 使用测试代码 alert() 或者使用一些调试器并在 show() 功能。

        9
  •  0
  •   user160820    15 年前

    我认为你的代码在工作。但是您的tooptip div中没有任何内容,这就是为什么您觉得该div没有显示的原因。

    在你的分区里写点东西就行了。

    而secod部分,我的意思是单击hide按钮会自动隐藏。 这似乎是不可能的,就像在你的代码中一样。

        10
  •  0
  •   Peter Mortensen Abd Al-Kareem Attiya    15 年前

    既然你在使用火狐,你可以查看火狐的“错误消息控制台”。打开工具并从中选择它。从这里你可以看到你的javascript失败的地方,如果失败的话。

        11
  •  0
  •   Paul Fedory    15 年前

    您可以尝试将分号放在函数块的末尾,就在右大括号之后。

    我发现没有它们,我的函数就不能正常工作,并且有非常不寻常的行为。