简体   繁体   English

非常不寻常的JavaScript行为

[英]Very unusual JavaScript behaviour

Here is something that I've used for ages now and all of the sudden I get these strange behaviours, maybe someone knows the reason why? 这是我多年来一直使用的东西,突然间我得到了这些奇怪的行为,也许有人知道原因是什么? Here is my HTML: 这是我的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');" />

Here is the JavaScript below the HTML code: 这是HTML代码下面的JavaScript:

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

Now there shouldn't be anything wrong with this code. 现在这个代码应该没有任何问题。 It should hide or show tooltip on each button click. 它应该隐藏或显示每个按钮点击的工具提示。 Now the weird thing going on is when I click on the hide button it hides itself, and when I click on show nothing happens. 现在奇怪的事情就是当我点击它隐藏的隐藏按钮时,当我点击显示没有任何反应。 Hide is still hidden. 隐藏仍然隐藏。

Did anyone have similar problem? 有没有人有类似的问题? Is there a work-around for it? 它有解决方法吗? Maybe another approach for accomplishing the same thing (pure JavaScript)? 也许另一种方法来完成同样的事情(纯JavaScript)?

UPDATE: Changed it to block, still isn't working. 更新:将其更改为阻止,仍然无法正常工作。 I mean why would it hide the button I'm clicking on when there is no connection to that whatsoever? 我的意思是,为什么它会隐藏我点击的按钮,当没有任何连接时? I'm using latest Firefox by the way. 我顺便使用最新的Firefox。 And I added alert in the function same thing. 而且我在功能上加了警告同样的事情。 Here is re-written code: 这是重写的代码:

function hide(id)
{
    alert(id);
    document.getElementById(id).style.display = 'none';
}
function show(id)
{
    alert(id);
    document.getElementById(id).style.display = 'block';
}

There doesn't seem to be anything obvious that would make this not work properly. 似乎没有任何明显的东西可以使它不能正常工作。 I'm guessing that there is some kind of typo, unclosed tag or function, etc. Try adding an alert() to both functions to see if the functions are even being called properly. 我猜测有某种拼写错误,未封闭的标签或功能等。尝试在两个函数中添加一个alert()来查看函数是否被正确调用。

EDIT: Based on your latest edit to the question, I'm leaning toward there being another item on the page with the same ID. 编辑:根据您对该问题的最新编辑,我倾向于在页面上有另一个具有相同ID的项目。 See this question . 看到这个问题

One other thing to try: in your show function, after setting the display property, try doing an alert on the display property to see what it was actually set to: 另一件事尝试:在你的show的功能,设置后display属性,尝试做一个alertdisplay性能,看看它实际上是设置为:

alert(document.getElementById(id).style.display);

Works perfectly fine when I copy paste your code and load it in google chrome. 当我复制粘贴您的代码并将其加载到谷歌浏览器时,工作完全正常。 You probably have a typo in the original page. 您可能在原始页面中输入了拼写错误。

我认为display =“none”的反面是display =“inline”或display =“block”,具体取决于你希望它出现的方式

我没有看到您编写的代码有任何特别的错误,但您可能想尝试在show函数上将display设置为'block'。

我打赌啤酒有两个ID为“工具提示”的元素。

You need to set the display type back to block: 您需要将显示类型设置回块:

<script type="text/javascript">

              function hide(id)
              {
              document.getElementById(id).style.display = 'none';
              }
              function show(id)
              {
              document.getElementById(id).style.display = 'block';                
              }

    </script>

You probably want to switch to 'block', when showing it, as others have remarked. 您可能希望在显示时切换到“阻止”,正如其他人所说的那样。 Other than that, if it is hidden by default, it has to be hidden by inline styles. 除此之外,如果它默认隐藏,则必须由内联样式隐藏。 If you hide it with code placed in an external css file, you will not be able to show it again with javascript. 如果您使用放在外部css文件中的代码隐藏它,您将无法使用javascript再次显示它。

  1. Check that display='block' thing - maybe your css specifies display='none' for class .tooltip ? 检查display='block'事情 - 也许你的css为class .tooltip指定display='none'

  2. Test your code using alert() , or use some debugger and set breakpoint in the show() function. 使用alert()测试代码,或者在show()函数中使用一些调试器和设置断点。

I think your Code is working. 我认为您的准则正在运作。 But your tooptip DIV has nothing in it that is why you feel that the DIV is not displayed. 但是你的tooptip DIV没有任何内容,这就是你觉得DIV没有显示的原因。

Just Write something in your DIV. 只需在你的DIV中写一些东西。

And the secod part, I mean By clicking hide button heides itself. 和secod部分,我的意思是点击隐藏按钮自己。 It seems to be impossible as in your code seems. 这似乎是不可能的,因为在您的代码中似乎。

Since you're using Firefox you can check Firefox's 'Error Message console'. 由于您使用的是Firefox,因此可以查看Firefox的“错误消息控制台”。 Open up Tools and choose it from there. 打开工具并从那里选择它。 From here you can see where your JavaScript fails, if it fails. 从这里你可以看到你的JavaScript失败的地方,如果它失败了。

You could try putting semicolons on the end of the function blocks, just after the closing brace. 您可以尝试在功能块的末尾添加分号,就在结束括号之后。

I found that without them, my functions weren't working correctly and had very unusual behaviour. 我发现没有它们,我的功能不正常,并且有非常不寻常的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM