disable all the elements in html

后端 未结 8 1220
清歌不尽
清歌不尽 2020-12-14 09:13

How can we disable all the elements in html through javascript.The easiest way...

相关标签:
8条回答
  • 2020-12-14 09:28

    I suggest to do it the "Lightbox"-style way.

    Add an absolute positioned, transparent, full screen div Layer above the Page. This way, the user can't even click on a Link.

    To give the user a visual feedback that the page is disabled, you can make the div e. g. 50% transparent black.

    BTW, here is also a jQuery Plugin that uses a similar technique.

    0 讨论(0)
  • 2020-12-14 09:31

    Just and without crutches!

    /**
     * Enable/disable all form controlls
     * @param status Status: true - form active, false - form unactive
     */
    HTMLFormElement.prototype.setStatus = function (status) {
        for (var i in this.elements) {
            this.elements[i].disabled = !status;
        }
    };
    
    // Example:
    var my_form = document.getElementById('my_form_with_many_inputs');
    my_form.setStatus(false); // Disable all inputs in form
    my_form.setStatus(true); // Enable all inputs in form
    
    0 讨论(0)
  • 2020-12-14 09:32

    I don't know why you would need that but this will work:

    // this will disable all input elements
    var elems = document.getElementsByTagName('input');
    var len = elems.length;
    
    for (var i = 0; i < len; i++) {
        elems[i].disabled = true;
    }
    
    0 讨论(0)
  • 2020-12-14 09:33

    Try this,

    function disableForm(theform) {
            if (document.all || document.getElementById) {
                for (i = 0; i < theform.length; i++) {
                var formElement = theform.elements[i];
                    if (true) {
                        formElement.disabled = true;
                    }
                }
            }
        }
    

    Or else you can try this too, as RaYell said

    function disableForm() {
        var inputs = document.getElementsByTagName("input");
        for (var i = 0; i < inputs.length; i++) {
            inputs[i].disabled = true;
        }
        var selects = document.getElementsByTagName("select");
        for (var i = 0; i < selects.length; i++) {
            selects[i].disabled = true;
        }
        var textareas = document.getElementsByTagName("textarea");
        for (var i = 0; i < textareas.length; i++) {
            textareas[i].disabled = true;
        }
        var buttons = document.getElementsByTagName("button");
        for (var i = 0; i < buttons.length; i++) {
            buttons[i].disabled = true;
        }
    }
    

    To disable the whole page you can find some info here,

    0 讨论(0)
  • 2020-12-14 09:34

    The easiest way is to put all form elements you want to disable inside a <fieldset> and then disable the fieldset itself.

    An example: http://jsfiddle.net/xdkf9b8j/1/

    If you don't want the border around the fieldset, remove it per css.

    0 讨论(0)
  • 2020-12-14 09:48

    Once i had to create a tutorial for my website. I needed to disable all interactions on a page excluding some elements. To do so i used this method: First make sure to remove all events bindings from your page elements. You can do this by using:

       $('*').unbind();
    

    Next disable all links on your page:

       $('a').each(function(){$(this).click(function(){return false;})});
    

    and disable all inputs:

       $('input').attr('disabled', true);
    

    The code needs to be executed at the end of your document. BTW you may exclude some elements within jquery selector to keep them active.

    0 讨论(0)
提交回复
热议问题