javascript - 在 html5 的支持下隐藏显示

标签 javascript jquery css html

我需要一些帮助。当我单击 THIS IS 1 和 THIS IS 2 时,输入框出现 我的问题是。我想单击 THIS IS 1 并将输入框带到 THIS IS 1 而不是单击 THIS IS 2 并且仍然将输入框带到 THIS IS 1。所以我想当我单击 THIS IS 1 时输入框出现在 THIS IS 1 下面当我单击 THIS IS 2 输入框下方是 THIS IS 2

<!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>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>

    </head>

    <body>
    <span onclick="MFunction()">THIS IS 1</span>
    <p></p>
    <span onclick="MFunction()">THIS IS 2</span>
    <div id="showhide" style="display:none;">
                        <input type="text"  required>
                        <br></br>
                    <button  class="btn btn-md-2 btn-primary">submit</button> 
                    </div>
    <script>
    function MFunction() {
        var x = document.getElementById("showhide");
        if (x.style.display === "none") {
            x.style.display = "block";
        } else {
            x.style.display = "none";
        }
    }
    </script>
    </body>
    </html>

最佳答案

如果你想使用相同的 showhide div 显示在被点击的元素下,你可能需要在点击时使用 insertBefore

请参阅下面的代码段:

function MFunction(event) {
        var x = document.getElementById("showhide");
        if (x.style.display === "none") {
            x.style.display = "block";
        }
        
        x.parentNode.insertBefore(x, event.target.nextSibling);
    }
    document.addEventListener('click', function(event){
    	if(event.target.className.includes("thisis")){
      	MFunction(event);
      }
    });
    
    //document.getElementById("thisis1").addEventListener("click", MFunction);
    //document.getElementById("thisis2").addEventListener("click", MFunction);
<!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>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>

    </head>

    <body>
    <span id="thisis1" class="thisis">THIS IS 1</span>
    <p></p>
    <span id="thisis2" class="thisis">THIS IS 2</span>
    <p></p>
    <span id="thisis3" class="donotshow">DO NOT SHOW ON THIS IS</span>
    <div id="showhide" style="display:none;">
                        <input type="text"  required>
                        <br><br>
                    <button  class="btn btn-md-2 btn-primary">submit</button> 
                    </div>
    </body>

也可以测试一下here

更新 1:

我已经更新了为多个元素添加单击事件的答案。你可以测试一下here

关于javascript - 在 html5 的支持下隐藏显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53071759/

相关文章:

jquery - Slim PHP 返回 JSON

javascript - Jquery:使用这个内部类内部函数,作为处理程序

php - 这种语法在 PHP 中的特殊用途? (三重 'Angle Brackets')

javascript - 如何使用原始 AJAX 将数据从客户端传递到服务器?

jquery - 如何使用 jQuery 的 on() 函数和过滤器获取原始选择器?

javascript - 为什么我不能放线性渐变?

html - 为什么绝对 div 在居中的 div 的左侧而不是右侧消失?

html - 如何让图像在 3 列网格上跨越 100% 的 div?

javascript - 如何在 aws 中重新部署我的 github 项目以查看更改?

javascript - 在 JavaScript 谷歌地图的中心显示十字准线