时光沉淀

原生js实现点击按钮显示/隐藏图片

· Views · By 浪子

想做一个打赏按钮,点击就能显示二维码 于是就有了以下的代码


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>显示和隐藏图片</title>
</head>
<body>
    <button id="btn" style="margin-left: 230px;">打赏</button>
</br>
    <img style="margin-top: 10px;display: none;" src="https://blogcdn.loliko.cn/pay/wx.png" width="500px" height="500px"> 

    <script>
        // 1.获取事件源
        var btn = document.getElementById("btn");
        var img = document.getElementsByTagName("img")[0];
        // var isShow = true;
        // 2.绑定事件
        btn.onclick = function() {
            // 3. 事件驱动程序
            if(btn.innerHTML === "关闭"){
                img.style.display = "none";
                btn.innerHTML = "打赏";
                // isShow = false;
            } else {
                img.style.display = "block";
                btn.innerHTML = "关闭";
                // isShow = true;
            }
        }
    </script>
</body>
</html>
none

评论

0 条评论

暂无评论

Using $this when not in object context

Error: Using $this when not in object context in /app/data/plugins/FediverseSync/template/comment.php:3 Stack trace: #0 /app/data/plugins/FediverseSync/Plugin.php(989): require_once() #1 [internal function]: FediverseSync_Plugin::renderComments(Object(Widget\Archive), NULL) #2 /app/var/Typecho/Plugin.php(446): call_user_func_array(Array, Array) #3 /app/var/Widget/Archive.php(1274): Typecho\Plugin->__call('Widget_Archive:...', Array) #4 /app/data/themes/paper-main/footer.php(23): Widget\Archive->footer() #5 /app/var/Widget/Archive.php(1336): require('/app/data/theme...') #6 /app/data/themes/paper-main/post.php(51): Widget\Archive->need('footer.php') #7 /app/var/Widget/Archive.php(1418): require_once('/app/data/theme...') #8 /app/var/Typecho/Router.php(99): Widget\Archive->render() #9 /app/index.php(23): Typecho\Router::dispatch() #10 {main}