简体   繁体   English

专注于非输入/锚元素?

[英]Focus on a non-input/anchor element?

I'm working on some JavaScript that requires a tag that can get focus. 我正在研究一些需要可以获得焦点的标记的JavaScript。 I'd like to be able to use a <div /> tag, however <div /> tags can't get focus. 我希望能够使用<div />标签,但<div />标签无法获得焦点。

It looks like the only elements that can gain focus are <input /> tags and <a /> tags. 看起来唯一可以获得焦点的元素是<input />标签和<a />标签。 Is there any other way to allow an element to gain focus that isn't an <input /> or <a /> tag? 有没有其他方法可以让元素获得不是<input /><a />标记的焦点?

I can't use <a /> or <input /> tags because I need to be able to place content inside of the tag so neither of those tags will work, unless there's a way to allow nested <a /> tags, although I doubt it since that goes against the standard . 我不能使用<a /><input />标签,因为我需要能够将内容放在标签内,这样这些标签都不会起作用,除非有办法允许嵌套的<a />标签,尽管我怀疑它,因为这违背了标准 I'm trying to figure out a way to allow a <div /> tag (or any other container element) to get focus. 我试图想出一种方法来允许<div />标签(或任何其他容器元素)获得焦点。

What do you mean by "can get focus"? 你能说“关注”是什么意思? Any DOM element can be setup to receive a multitude of javascript events including click events. 可以设置任何DOM元素以接收包括click事件在内的大量javascript事件。

Or do you mean "can be tabbed to with the keyboard"? 或者你的意思是“可以用键盘选项卡”? If so, and if you cannot use an a tag as your container, then try the tabindex property on your elements. 如果是这样,如果你不能使用a标签为您的容器,然后尝试tabindex属性上的元素。 I'm not sure how cross-browser it is, but at least try it before writing a tabbed ui in javascript yourself. 我不确定它是如何跨浏览器的,但至少在自己用javascript编写标签式ui之前先尝试一下。

Bookmarks are the way to go. 书签是要走的路。

Begin your content with 开始你的内容

<a href="#anchor">

Somewhat of a workaround and not semantically correct, but... 有些解决方法而且在语义上不正确,但......

a#foo { display: block; }
a#foo:hover { cursor: default; }

Then use JavaScript to intercept the onClick event on a#foo and return false. 然后使用JavaScript拦截在onClick事件a#foo并返回false。

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

相关问题 在选择非输入元素时,强制焦点在iOS中使用jQuery保留表单字段 - force the focus to leave a form field with jQuery in iOS when selecting non-input element 在非输入元素中捕获“ Selstart”,“ Sel..end” - Capturing “Selstart”, “Sel..end” within a non-input element contenteditable 的 html 数据列表(非输入标签) - html datalist for contenteditable (non-input tag) 在非输入元素上使用 v-model 并将其绑定到 innerHTML 值 - Use v-model on a non-input element and bind it to the innerHTML value 验证非输入字段的角度 - Validation of non-input field in angular AngularJS-从非输入元素获取值以通过列表进行过滤 - AngularJS - Get value from non-input element to filter through list 绑定输入和非输入数字内容以计算一系列值 - binding input and non-input numeric contents for calculation of a series of values 如何将form_for与非输入元素一起使用? - How do I use form_for with non-input elements? 在jquery验证期间向非输入元素添加样式 - Adding styling to non-input elements during jquery validation 以表格形式获取非输入字段,例如boostrap datepicker - GET a non-input field such as the boostrap datepicker in a form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM