简体   繁体   English

将键盘焦点设置为<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>

I have the following code snippet:我有以下代码片段:

<div id="listbox1div" style="z-index:95; background:white; overflow-y:auto; overflow-x:hidden; width:240; height:314px;">
<a id="focusLink2"></a>
<table id="ptObj_listbox1...

I have a page that is building <div> elements dynamically (such as above).我有一个动态构建<div>元素的页面(例如上面)。 This <div> displays data on top of the main screen.这个<div>在主屏幕顶部显示数据。 When the page generates the divs I would like to set focus.当页面生成 div 时,我想设置焦点。 I can not put an onLoad function on the body tag as I don't know when the divs will be generated.我不能将 onLoad function 放在 body 标签上,因为我不知道何时会生成 div。

A <div> tag can not have focus set on it directly. <div>标签不能直接设置焦点。 So I put an empty <a> tag with an id that I'm calling in the following function:所以我在下面的 function 中放置了一个带有 id 的空<a>标签:

function setTableFocus(count){
        var flinkText = 'focusLink'+count;
       document.getElementById(flinkText).focus();
}

I'm not getting any errors and I know the function is being called when the page is presented (via alerts).我没有收到任何错误,并且我知道在显示页面时(通过警报)正在调用 function。 However, when I using the arrow keys or enter button the entire page moves (not even the div that is presenting the data).但是,当我使用箭头键或输入按钮时,整个页面都会移动(甚至不是显示数据的 div)。

When I click on to one of the table elements (using the mouse).当我单击其中一个表格元素时(使用鼠标)。 After that the keydown event starts working.之后,keydown 事件开始工作。 What I would like this to do is to present the data to the user and automatically be keyboard driven.我想做的是将数据呈现给用户并自动由键盘驱动。

Does anyone have any suggestions how I can accomplish this?有人对我如何做到这一点有任何建议吗?

you can make a div focusable if you add a tabindex attribute.如果添加tabindex属性,则可以使div具有焦点。

see: http://snook.ca/archives/accessibility_and_usability/elements_focusable_with_tabindex见: http://snook.ca/archives/accessibility_and_usability/elements_focusable_with_tabindex

The tabindex value can allow for some interesting behaviour. tabindex 值可以允许一些有趣的行为。

  • If given a value of "-1", the element can't be tabbed to but focus can be given to the element programmatically (using element.focus()).如果给定值“-1”,则元素不能被制表符,但焦点可以通过编程方式赋予元素(使用 element.focus())。
  • If given a value of 0, the element can be focused via the keyboard and falls into the tabbing flow of the document.如果给定值 0,则元素可以通过键盘获得焦点并落入文档的选项卡流中。
  • Values greater than 0 create a priority level with 1 being the most important.大于 0 的值创建优先级,其中 1 是最重要的。

UPDATE: added a simple demo at http://jsfiddle.net/roberkules/sXj9m/更新:http://jsfiddle.net/roberkules/sXj9m/添加了一个简单的演示

The function that's dynamically generating the divs will have the context available to know which div to focus on, after the last div output a script with a scrollTo() to focus on the div you want.动态生成 div 的 function 将具有可用于知道要关注哪个 div 的上下文,在最后一个div output 之后,一个带有 scrollTo() 的脚本可以专注于您想要的 div。 Assign each div an ID, so you'll be able to choose it out of the set.为每个 div 分配一个 ID,这样您就可以从集合中选择它。

Response.Write "
<script language='text/javascript'>
document.getElementById('div#').scrollIntoView();
</script>
"

I solve that doing the next:我解决了下一个问题:

<div tabindex="0" >
    <button onclick="element.parentNode.focus();"/>
</div>

You can't focus a div.你不能集中一个div。 Do you mean focus an input?你的意思是关注输入吗?

Anyway, you can include a short script tag to focus something right in the HTML - simply put the script right after the div - or even inside the div.无论如何,您可以包含一个简短的脚本标签,以便在 HTML 中正确关注某些内容 - 只需将脚本放在 div 之后 - 甚至放在 div 内。

Are you in control of the asp function?你在控制asp function吗? If yes, then that'd be the easiest place to set focus.如果是,那将是最容易设置焦点的地方。 Otherwise, you could listen to DOMNodeInserted event (note: browser support may vary) and set focus to div (or its children) based on appropriate conditions.否则,您可以侦听DOMNodeInserted事件(注意:浏览器支持可能会有所不同)并根据适当的条件将焦点设置为div (或其子级)。

You could use getElementsByClassName您可以使用getElementsByClassName

<div tabindex="0" class="to-focus">TEXT</div>
<script> document.getElementsByClassName('to-focus')[0].focus()</script>

do you mean you want to focus the divs whenever it is generated?你的意思是你想在生成div时关注它吗? try reasearching for these尝试研究这些

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

and

http://api.jquery.com/live/ http://api.jquery.com/live/

and for the keypress..和按键..

http://api.jquery.com/keypress/ http://api.jquery.com/keypress/

afaik divs and other elements can have focus of some sort like in wikis, like.. afaik div 和其他元素可以具有某种焦点,例如在 wiki 中,例如..

http://en.wikipedia.org/wiki/JavaScript#Cross-site_vulnerabilities http://en.wikipedia.org/wiki/JavaScript#Cross-site_vulnerabilities

and it'll scroll automatically there.它会在那里自动滚动。 I am just no sure how.我只是不知道怎么做。

as for the dynamically generated divs and tables you can use jquery's live() function至于动态生成的 div 和表,您可以使用 jquery 的 live() function

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM