document.getElementById(“test”).style.display=“hidden” not working

后端 未结 11 964
没有蜡笔的小新
没有蜡笔的小新 2021-02-02 14:10

I want to hide my form when I click on the submit button. My code is as follows:



        
相关标签:
11条回答
  • 2021-02-02 14:53

    Using jQuery:

       $('#test').hide();
    

    Using Javascript:

    document.getElementById("test").style.display="none";
    

    Threw an error "Cannot set property 'display' of undefined"

    So, fix for this would be:

    document.getElementById("test").style="display:none";
    

    where your html code will look like this:

    <div style="display:inline-block" id="test"></div>
    
    0 讨论(0)
  • 2021-02-02 14:53

    this should be it try it.

    document.getElementById("test").style.display="none"; 
    
    0 讨论(0)
  • 2021-02-02 14:55

    There are two ways of doing this.

    Most of the answers have correctly pointed out that style.display has no value called "hidden". It should be none.

    If you want to use "hidden" the syntax should be as follows.

    object.style.visibility="hidden"
    

    The difference between the two is the visibility="hidden" property will only hide the contents of you element but retain it position on the page. Whereas the display ="none" will hide your complete element and the rest of the elements on the page will fill that void created by it.

    Check this illustration

    0 讨论(0)
  • 2021-02-02 14:57

    Maybe you can add a class like 'hide'.

    Follow the example here : https://developer.mozilla.org/fr/docs/Web/API/Element/classList.

    document.getElementById("test").classList.add("anotherclass");

    0 讨论(0)
  • 2021-02-02 15:00

    Through JavaScript

    document.getElementById("test").style.display="none";
    

    Through Jquery

    $('#test').hide();
    
    0 讨论(0)
提交回复
热议问题