【问题标题】:html click button display/hide picture? [duplicate]html点击按钮显示/隐藏图片? [复制]
【发布时间】:2013-05-12 12:51:05
【问题描述】:

所以,我是 html 新手,我说我有 2 个按钮,分别名为“显示图片 1”和“显示图片 2”

我希望点击“显示图片1”按钮后能够显示图片1。

我还希望能够在 Picture1 所在的位置显示 Picture2(如果正在显示 Picture1,则隐藏 Picture1)。

我应该编写什么代码才能让这种情况发生?

到目前为止,我用于显示图片 1 的代码:

<style type="text/css">
.show{display:block;}
.hide{display:none;}
</style>
<script type="text/javascript">
function showImg()
{
var obj=document.getElementById('Picture1');
obj.className = 'show';
}
</script>



<center>
<img id="Picture1" src=Picture1.jpg" class="hide">
<input type="button" onclick = "showImg()" value= "Picture1">
</center>

谢谢

【问题讨论】:

    标签: javascript html toggle


    【解决方案1】:

    /// 如果你想切换图像试试这个简单的代码////

    <script type = "text/javascript">
            function pic1()
            {
                document.getElementById("img").src = "picture 1 source";
            }
            function pic2()
            {
                document.getElementById("img").src ="picture 2 source";
            } </script>
    
    
    
    
    
        <img src = "" id = "img"/> <input type="button" value="Show Picture
        1" onclick="pic1()"/> <input type="button" value="Show Picture 2"
        onclick="pic2()"/>
    

    【讨论】:

    • 谢谢您,我现在正在使用它,但我想知道如何添加替代文本以防图像无法显示?
    • 如果找不到图像则显示文本或使图像源为空?
    • 例如,如果图像无法/无法加载,则“无法加载图像”
    猜你喜欢
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多