Javascript 单击时显示/隐藏图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1999071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:19:35  来源:igfitidea点击:

show/hide image on click

javascripthtml

提问by crazyTechie

I need to show/hide image in html page. I thought its very simple. But why I am getting error 'visible' undefined.

我需要在 html 页面中显示/隐藏图像。我以为很简单。但是为什么我收到错误“可见”未定义。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Ajax Test
    </title>
    <script type="text/javascript">
<!--
    function showImage(){
        document.getElementById('loadingImage').style.visibility=visible;
    }

    -->

    </script>
    </head>
<body>
    <input type="button" value="Ajax Button" onclick="showImage();"/>
    <img id="loadingImage" src="ajax-loader.gif" style="visibility:hidden"/>

</body>

回答by David M

You need to put it in quotes - it's a string:

你需要把它放在引号中 - 它是一个字符串:

document.getElementById('loadingImage').style.visibility='visible';

回答by TIMEX

I would use Jquery. Go download it at the Jquery home page.

我会使用 Jquery。去Jquery主页下载它。

Then, include it:

然后,包括它:

<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
function showImage(){
$("#loadingImage").toggle();
}

</script>


<img id="loadingImage" src="ajax-loader.gif" style="display:none;"/>

回答by Phil.Wheeler

If the other answers don't give you the results you're after, try setting the display to none:

如果其他答案没有给出您想要的结果,请尝试将显示设置为无:

document.getElementById('loadingImage').style.display='none';

回答by crazyTechie

I am very very sorry. It should be

非常非常抱歉。它应该是

  document.getElementById('loadingImage').style.visibility='visible';

quuotes missing aroung visible.

报价丢失周围可见。

回答by Rich Adams

You need to enclose it in quote marks, otherwise JavaScript thinks you're trying to set it to be the value of a variable called "visible". Since you don't have a variable called "visible", you get the error saying that it's undefined.

您需要将它括在引号中,否则 JavaScript 会认为您正在尝试将其设置为名为“visible”的变量的值。由于您没有名为“visible”的变量,因此您会收到错误消息,指出它未定义。

document.getElementById('loadingImage').style.visibility='visible';