简体   繁体   English

按住按钮点击div

[英]Hiding div on button click

I have a script which has three buttons which shows a div when clicked, now my question is how do I hide those div's so let's say div 1 is opened and I click to open div 2, then make it show div 2 but hide div 1 so that I can have the div's be in the same position, but they only show if they are supposed to. 我有一个脚本,有三个按钮,点击时显示div,现在我的问题是我如何隐藏这些div所以让我们说div 1打开然后我点击打开div 2,然后让它显示div 2但是隐藏div 1所以我可以让div处于相同的位置,但是他们只能表明他们是否应该这样做。

My script: 我的剧本:

  <!-- 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 part: css部分:

#contactSupportForm{
    display: none;
}

#contactBusinessForm{
    display: none;
}

#contactOtherForm{
    display: none;
}

Here is a JSFiddle to show the whole process of how it works. 这是一个JSFiddle来展示它如何工作的整个过程。 http://jsfiddle.net/m0jdv6u0/3/ http://jsfiddle.net/m0jdv6u0/3/

You could try this: 你可以试试这个:

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"
}

And you call in each button passing the id of the div, like this: 你调用每个按钮传递div的id,如下所示:

showOtherForm('contactOtherForm')

Of course, you can make some verifications, so you don't need to set the display on the 3 divs, but I think you dont need that... 当然,您可以进行一些验证,因此您不需要在3个div上设置显示,但我认为您不需要...

Hope it helped! 希望它有所帮助!

There's probably a more elegant way to use classes as selectors and achieve the same functional goal, but here's a solution that would enable you to add additional form / button elements without having to add additional show/hide blocks: 可能有一种更优雅的方式将classes用作选择器并实现相同的功能目标,但这里的解决方案可以让您添加其他表单/按钮元素而无需添加其他显示/隐藏块:

[Note - this is not significantly different from @Cleversou's answer] [注意 - 这与@ 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