繁体   English   中英

按住按钮点击div

[英]Hiding div on button click

我有一个脚本,有三个按钮,点击时显示div,现在我的问题是我如何隐藏这些div所以让我们说div 1打开然后我点击打开div 2,然后让它显示div 2但是隐藏div 1所以我可以让div处于相同的位置,但是他们只能表明他们是否应该这样做。

我的剧本:

  <!-- SUPPORT CONTACT FORM START-->
            <div class="contactSupportButton"><input type="button" src=".png" alt="contact support button" style="height: 40px; width: 120px" onClick="showSupForm()"/>
                    <div id="contactSupportForm">
                        TEST
                    </div>
                </div>

            <script type="text/javascript">
                function showSupForm() {
                document.getElementById('contactSupportForm').style.display = "block";
                }
            </script>
            <!-- SUPPORT CONTACT FORM ENDING-->

            <!-- BUSINESS CONTACT FORM START-->
            <div class="contactBusinessButton"><input type="button" src=".png" alt="contact business button" style="height: 40px; width: 120px" onClick="showBusForm()"/>
                    <div id="contactBusinessForm"> 
                        TEST
                    </div>
                </div>

            <script type="text/javascript">
                function showBusForm() {
                document.getElementById('contactBusinessForm').style.display = "block";
                }
            </script>
            <!-- BUSINESS CONTACT FORM ENDING-->

            <!-- OTHER CONTACT FORM START-->
            <div class="contactOtherButton"><input type="button" src=".png" alt="contact other button" style="height: 40px; width: 120px" onClick="showOtherForm()"/>
                    <div id="contactOtherForm">
                        TEST
                    </div>
                </div>

            <script type="text/javascript">
                function showOtherForm() {
                document.getElementById('contactOtherForm').style.display = "block";
                }
            </script>
            <!-- OTHER CONTACT FORM ENDING-->

css部分:

#contactSupportForm{
    display: none;
}

#contactBusinessForm{
    display: none;
}

#contactOtherForm{
    display: none;
}

这是一个JSFiddle来展示它如何工作的整个过程。 http://jsfiddle.net/m0jdv6u0/3/

你可以试试这个:

function showOtherForm(idElement) {
    document.getElementById('contactOtherForm').style.display = "none";
    document.getElementById('contactSupportForm').style.display = "none";
    document.getElementById('contactBusinessForm').style.display = "none"
    document.getElementById(idElement).style.display = "block"
}

你调用每个按钮传递div的id,如下所示:

showOtherForm('contactOtherForm')

当然,您可以进行一些验证,因此您不需要在3个div上设置显示,但我认为您不需要...

希望它有所帮助!

可能有一种更优雅的方式将classes用作选择器并实现相同的功能目标,但这里的解决方案可以让您添加其他表单/按钮元素而无需添加其他显示/隐藏块:

[注意 - 这与@ Cleversou的答案没有太大区别]

 function showForm(elemId){ var arr = ["Other", "Business", "Support"] ; for(var i = 0; i < arr.length; i++){ if(elemId === "contact" + arr[i] + "Form"){ document.getElementById(elemId).style.display = "block"; } else { document.getElementById("contact" + arr[i] + "Form").style.display = "none"; } } } 
 #contactSupportForm{ display: none; } #contactBusinessForm{ display: none; } #contactOtherForm{ display: none; } 
 <!-- SUPPORT CONTACT FORM START--> <div class="contactSupportButton"><input type="button" src=".png" alt="contact support button" style="height: 40px; width: 120px" onClick="showForm('contactSupportForm')"/> <div id="contactSupportForm"> TEST </div> </div> <script type="text/javascript"> </script> <!-- SUPPORT CONTACT FORM ENDING--> <!-- BUSINESS CONTACT FORM START--> <div class="contactBusinessButton"><input type="button" src=".png" alt="contact business button" style="height: 40px; width: 120px" onClick="showForm('contactBusinessForm')"/> <div id="contactBusinessForm"> TEST </div> </div> <script type="text/javascript"> </script> <!-- BUSINESS CONTACT FORM ENDING--> <!-- OTHER CONTACT FORM START--> <div class="contactOtherButton"><input type="button" src=".png" alt="contact other button" style="height: 40px; width: 120px" onClick="showForm('contactOtherForm')"/> <div id="contactOtherForm"> TEST </div> </div> <script type="text/javascript"> </script> <!-- OTHER CONTACT FORM ENDING--> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM