gpt4 book ai didi

javascript - 单击链接时如何显示字段? (javascript)

转载 作者:行者123 更新时间:2023-11-28 02:55:26 24 4
gpt4 key购买 nike

如何创建它,以便当我单击“报告用户”时,会显示一个框,其中显示报告用户的原因列表和提交按钮。

我很少使用 javascript...有人能指出我正确的方向吗?

最佳答案

基本方法是使用 Javascript 切换 CSS 显示。这是以下代码的分解:

  1. 在页面加载时将事件附加到链接。这就是 window.onload 部分的作用。
  2. 使用 document.getElementById 识别链接和框
  3. 使用匿名函数捕获显示切换
  4. 使用 style.display 切换显示。

    <!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" xml:lang="en" lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Onclick Example</title>
    <script type="text/javascript">
    window.onload = function(){
    var link = document.getElementById('rulink');
    var box = document.getElementById('box');
    var close = document.getElementById('close');
    link.onclick = function(){
    box.style.display = 'block'
    }
    close.onclick = function(){
    box.style.display = 'none';
    }
    }
    </script>
    <style>
    div{
    display:none;
    background:#f00;
    width:100px;
    }
    </style>
    </head>
    <body>
    <a href="javascript:void(0)" id="rulink">report user</a>
    <div id="box">
    <ul>
    <li>abc</li>
    <li>def</li>
    </ul>
    <a href="javascript:void(0)" id="close">Close</a>
    </div>
    </body>

关于javascript - 单击链接时如何显示字段? (javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2785386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com