jquery : focus to div is not working

前端 未结 3 516
余生分开走
余生分开走 2020-12-01 08:57

After the ajax function is over. in success messages I\'m focusing to the specific div. But it\'s not working. My code is here.

$j.ajax({
    url:\"

        
相关标签:
3条回答
  • 2020-12-01 09:30

    you can use the below code to bring focus to a div, in this example the page scrolls to the <div id="navigation">

    $('html, body').animate({ scrollTop: $('#navigation').offset().top }, 'slow');
    
    0 讨论(0)
  • 2020-12-01 09:39

    a <div> can be focused if it has a tabindex attribute. (the value can be set to -1)

    For example:

    $("#focus_point").attr("tabindex",-1).focus();
    

    In addition, consider setting outline: none !important; so it displayed without a focus rectangle.

    var element = $("#focus_point");
    element.css('outline', 'none !important')
           .attr("tabindex", -1)
           .focus();
    
    0 讨论(0)
  • 2020-12-01 09:40

    Focus doesn't work on divs by default. But, according to this, you can make it work:

    The focus event is sent to an element when it gains focus. This event is implicitly applicable to a limited set of elements, such as form elements (<input>, <select>, etc.) and links (<a href>). In recent browser versions, the event can be extended to include all element types by explicitly setting the element's tabindex property. An element can gain focus via keyboard commands, such as the Tab key, or by mouse clicks on the element.

    http://api.jquery.com/focus/

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