how to make a DIV unfocusable?

前端 未结 5 1601
情深已故
情深已故 2021-02-07 10:16

I met a problem about HTML rendering.

In dir=\"rtl\" document of IE7, when JavaScript tries to set focus to a DIV element(with oElement.focus() method), the rendering tu

相关标签:
5条回答
  • 2021-02-07 10:43

    The <div> should not be capable of receiving focus unless you have added tabIndex.

    If you have added tabIndex, you should remove it by

    document.getElementById("yourElement").removeAttribute("tabIndex");
    
    0 讨论(0)
  • 2021-02-07 10:46

    An easy fix could be done by adding CSS :

    div_selector{
        pointer-events: none;
    }
    

    Or By adding same css via jQuery :

    $(div_selector).css('pointer-events', 'none');

    0 讨论(0)
  • 2021-02-07 10:48

    I'm not sure if you can make an element 'un-focusable', but you can certainly un-focus it at a specific point in time using its blur method:

    document.getElementById("myElement").blur();
    

    EDIT:

    I think you can make an element 'un-focusable' by defocusing it every time it is focused. You can accomplish this via:

    document.getElementById("myElement").onfocus = function() {
        this.blur();
    };
    

    ...or (using inline Javascript in your HTML):

    <div onfocus="this.blur();"></div>
    

    Steve

    0 讨论(0)
  • 2021-02-07 10:49

    Below is the some way to make DIV unfocusable using jquery.

        //  Set the tabindex atribute to -1.
            1)$("divid").attr('tabindex','-1');
        //  Remove the tabindex atribute if exists.
            2) $("divid").removeAttr('tabindex');
       //   This is third way.
            3) $("divid").blur();
    
    0 讨论(0)
  • 2021-02-07 10:50

    Additionally, If you want to make a focussable element(form input elements etc.) as unfocussable. You can set :

    tabIndex = "-1"
    
    document.getElementById("yourElement").setAttribute("tabIndex", "-1");
    
    0 讨论(0)
提交回复
热议问题