将键盘焦点设置为

发布于 2024-11-24 19:09:03 字数 885 浏览 3 评论 0 原文

我有以下代码片段:

<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...

我有一个正在动态构建

元素的页面(如上面)。此
在主屏幕顶部显示数据。当页面生成 div 时,我想设置焦点。我无法在 body 标记上放置 onLoad 函数,因为我不知道何时生成 div。

标签不能直接将焦点设置在其上。因此,我放置了一个空的 标记,其中包含我在以下函数中调用的 id:

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

我没有收到任何错误,并且我知道在呈现页面时正在调用该函数(通过警报)。但是,当我使用箭头键或 Enter 按钮时,整个页面都会移动(甚至不包括显示数据的 div)。

当我单击其中一个表格元素时(使用鼠标)。之后 keydown 事件开始工作。我希望这样做是将数据呈现给用户并自动由键盘驱动。

有人对我如何实现这一目标有任何建议吗?

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). This <div> displays data on top of the main screen. When the page generates the divs I would like to set focus. I can not put an onLoad function on the body tag as I don't know when the divs will be generated.

A <div> tag can not have focus set on it directly. So I put an empty <a> tag with an id that I'm calling in the following function:

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). However, when I using the arrow keys or enter button the entire page moves (not even the div that is presenting the data).

When I click on to one of the table elements (using the mouse). After that the keydown event starts working. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

猫性小仙女 2024-12-01 19:09:03

如果添加 tabindex 属性,则可以使 div 可聚焦。

请参阅:http://snook.ca/archives/accessibility_and_usability/elements_focusable_with_tabindex

tabindex 值可以允许一些有趣的行为。

  • 如果给定值“-1”,则无法使用 Tab 键定位该元素,但可以获取焦点
    以编程方式赋予元素(使用 element.focus())。
  • 如果给定值为 0,则该元素可以通过键盘获得焦点,并且
    落入文档的选项卡流程中。
  • 大于 0 的值会创建一个优先级,其中 1 表示最重要。

更新:http://jsfiddle.net/rorkules/sXj9m/ 添加了一个简单的演示

you can make a div focusable if you add a tabindex attribute.

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

The tabindex value can allow for some interesting behaviour.

  • 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()).
  • If given a value of 0, the element can be focused via the keyboard and
    falls into the tabbing flow of the document.
  • Values greater than 0 create a priority level with 1 being the most important.

UPDATE: added a simple demo at http://jsfiddle.net/roberkules/sXj9m/

孤城病女 2024-12-01 19:09:03

动态生成 div 的函数将具有可用于知道要关注哪个 div 的上下文,在最后一个 div 输出一个带有scrollTo() 的 script 后,将焦点集中在你想要的div。为每个 div 分配一个 ID,以便您可以从集合中选择它。

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

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. Assign each div an ID, so you'll be able to choose it out of the set.

Response.Write "
<script language='text/javascript'>
document.getElementById('div#').scrollIntoView();
</script>
"
挽清梦 2024-12-01 19:09:03

我解决了这个问题,执行下一步:

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

I solve that doing the next:

<div tabindex="0" >
    <button onclick="element.parentNode.focus();"/>
</div>
罪歌 2024-12-01 19:09:03

你能控制asp功能吗?如果是,那么这将是最容易集中注意力的地方。否则,您可以侦听 DOMNodeInserted 事件(注意:浏览器支持可能会有所不同)并根据适当的条件将焦点设置为 div (或其子级)。

Are you in control of the 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.

又怨 2024-12-01 19:09:03

您可以使用 getElementsByClassName

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

You could use getElementsByClassName

<div tabindex="0" class="to-focus">TEXT</div>
<script> document.getElementsByClassName('to-focus')[0].focus()</script>
倾城泪 2024-12-01 19:09:03

你的意思是你想在div生成时聚焦它?
尝试重新搜索这些

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

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

和按键..

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

afaik div 和其他元素可以像 wiki 中那样具有某种焦点,例如..

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

它会自动滚动到那里。我只是不知道怎么做。

至于动态生成的div和表格,你可以使用jquery的live()函数

do you mean you want to focus the divs whenever it is generated?
try reasearching for these

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

and

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

and for the keypress..

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

afaik divs and other elements can have focus of some sort like in wikis, like..

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

合久必婚 2024-12-01 19:09:03

你不能聚焦一个div。您的意思是集中输入吗?

无论如何,您可以包含一个简短的脚本标签来将某些内容集中在 HTML 中 - 只需将脚本放在 div 后面 - 甚至放在 div 内部。

You can't focus a 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文