jquery:焦点到 div 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5965924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:06:21  来源:igfitidea点击:

jquery : focus to div is not working

jquery

提问by Arung

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.

ajax功能结束后。在成功消息中,我专注于特定的 div。但它不起作用。我的代码在这里。

$j.ajax({
    url:"<?php echo admin_url( 'admin-ajax.php' ); ?>",
    type:"POST",
    data:"action=press_release&page="+0+"&do_task="+do_task+"&id="+id+"&module="+module,
    success:function(data){
        $j("#com_cont").show();
        $j("#com_cont").html(data);
        $j("#loading_heart").hide();
        $j("#focus_point").focus();
    }
});

This is the code is not working(Not focusing on the div:$j("#focus_point").focus();

这是代码不起作用(不关注div:$j("#focus_point").focus();

回答by suDocker

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

一个<div>可如果它有一个集中tabindex的属性。(该值可以设置为-1)

For example:

例如:

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


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

此外,请考虑设置outline: none !important;使其在没有焦点矩形的情况下显示。

var element = $("#focus_point");
element.css('outline', 'none !important')
       .attr("tabindex", -1)
       .focus();

回答by derek

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

您可以使用以下代码将焦点移至 div,在此示例中,页面滚动到 <div id="navigation">

$('html, body').animate({ scrollTop: $('#navigation').offset().top }, 'slow');

回答by Amy Anuszewski

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

默认情况下,焦点不适用于 div。但是,根据this,您可以使其工作:

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.

当元素获得焦点时,将焦点事件发送到元素。此事件是隐式地适用于有限的一组元素,诸如表单元素(的<input><select>等)和链接(<a href>)。在最近的浏览器版本中,可以通过显式设置元素的 tabindex 属性将事件扩展为包括所有元素类型。元素可以通过键盘命令(例如 Tab 键)或鼠标单击元素获得焦点。

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

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