简体   繁体   English

如何在父div内为div设置焦点?

[英]How to set focus for div inside parent div?

<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#container2").focus();
        });
    </script>
</head>

<body>
    <div id="mainwrapper">
        <!-- container1 -->
        <div id="container1" 
             style="height:100px;width:299px;">
            First Lorem Ipsum is simply dummy text1 
        </div>

        <!-- container2 -->
        <div id="container2"  style="height:100px;width:299px;">
            Second Lorem Ipsum is simply dummy text2
        </div>

    </div>
</body>

this is my html code,when page loads focus should on second div,but this is not working ,help me how to do this,thanks in advance. 这是我的html代码,当页面加载时应将焦点放在第二个div上,但这不起作用,请提前谢谢我。

.focus() is not going to scroll to specified div.use: .focus()不会滚动到指定的div.use:

$(document).ready(function() {
   $('html, body').animate({ scrollTop: $('#container2').offset().top }, 'slow');
});

Working Demo 工作演示

This probably does what you need. 这可能会满足您的需求。 At first, set class="borderonload" to #container2 , then add the CSS below: 首先,将class="borderonload"#container2 ,然后添加以下CSS:

#mainwrapper div:hover, .borderonload {
    border: 2px solid #aaf;
}

In script: 在脚本中:

$('#mainwrapper').on('mousemove', function (e) {
    var $cont2 = $('#container2');
    if (e.target !== $cont2) {
        $cont2.removeClass('borderonload');
        $('#mainwrapper').off('mousemove');
    }
});

A live demo at jsFiddle . jsFiddle上的现场演示

将键盘焦点设置为<div></div><div id="text_translate"><p>我有以下代码片段:</p><pre> &lt;div id="listbox1div" style="z-index:95; background:white; overflow-y:auto; overflow-x:hidden; width:240; height:314px;"&gt; &lt;a id="focusLink2"&gt;&lt;/a&gt; &lt;table id="ptObj_listbox1...</pre><p> 我有一个动态构建&lt;div&gt;元素的页面(例如上面)。 这个&lt;div&gt;在主屏幕顶部显示数据。 当页面生成 div 时,我想设置焦点。 我不能将 onLoad function 放在 body 标签上,因为我不知道何时会生成 div。</p><p> &lt;div&gt;标签不能直接设置焦点。 所以我在下面的 function 中放置了一个带有 id 的空&lt;a&gt;标签:</p><pre> function setTableFocus(count){ var flinkText = 'focusLink'+count; document.getElementById(flinkText).focus(); }</pre><p> 我没有收到任何错误,并且我知道在显示页面时(通过警报)正在调用 function。 但是,当我使用箭头键或输入按钮时,整个页面都会移动(甚至不是显示数据的 div)。</p><p> 当我单击其中一个表格元素时(使用鼠标)。 之后,keydown 事件开始工作。 我想做的是将数据呈现给用户并自动由键盘驱动。</p><p> 有人对我如何做到这一点有任何建议吗?</p></div> - Set keyboard focus to a <div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将焦点设置为TextAngular的div - How to set focus to div of TextAngular 如何在父div中隐藏/显示div - How to hide/show a div inside a parent div 以编程方式在父div中滚动一组div - programmatically scrolling a set of divs inside a parent div IE 8 Jquery焦点问题父div内的子级 - IE 8 Jquery focus issue children inside parent div 将键盘焦点设置为<div></div><div id="text_translate"><p>我有以下代码片段:</p><pre> &lt;div id="listbox1div" style="z-index:95; background:white; overflow-y:auto; overflow-x:hidden; width:240; height:314px;"&gt; &lt;a id="focusLink2"&gt;&lt;/a&gt; &lt;table id="ptObj_listbox1...</pre><p> 我有一个动态构建&lt;div&gt;元素的页面(例如上面)。 这个&lt;div&gt;在主屏幕顶部显示数据。 当页面生成 div 时,我想设置焦点。 我不能将 onLoad function 放在 body 标签上,因为我不知道何时会生成 div。</p><p> &lt;div&gt;标签不能直接设置焦点。 所以我在下面的 function 中放置了一个带有 id 的空&lt;a&gt;标签:</p><pre> function setTableFocus(count){ var flinkText = 'focusLink'+count; document.getElementById(flinkText).focus(); }</pre><p> 我没有收到任何错误,并且我知道在显示页面时(通过警报)正在调用 function。 但是,当我使用箭头键或输入按钮时,整个页面都会移动(甚至不是显示数据的 div)。</p><p> 当我单击其中一个表格元素时(使用鼠标)。 之后,keydown 事件开始工作。 我想做的是将数据呈现给用户并自动由键盘驱动。</p><p> 有人对我如何做到这一点有任何建议吗?</p></div> - Set keyboard focus to a <div> JQuery:在单击父div时将焦点设置为select - JQuery: Set focus on select when parent div is clicked 如何在列中的div内设置div - how to set div inside div in columns 根据父div中剩余的空间设置div的初始高度 - Set initial height of div based on how much space is left inside of parent div 如何将“焦点”设置回到可疑的div - How to set “focus” back to contenteditable div 如何在 Javascript 的 iframe 中将焦点设置为 div 元素? - How to set the focus to a div element in an iframe in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM