我已经准备好了小提琴,请仔细研究。
https://jsfiddle.net/s7fxy8jp/
从小提琴中,有可能在文本Element Z(仅可聚焦脚本)的文本末尾闪烁普通光标,而不是背景色aqua。
的HTML
<div tabindex="-1" id="scripted">Element Z (script-only focusable)</div>
<div id="test">Set Focus To Element Z</div>
的CSS
div:focus {
background-color: Aqua;}
的JavaScript
document.getElementById('test').onclick = function () {
document.getElementById('scripted').focus();};
最佳答案
您需要使用contenteditable
属性允许编辑div内容。
<div tabindex="-1" id="scripted" contenteditable="true">Element Z (script-only focusable)</div>
<div id="test">Set Focus To Element Z</div>
这将使您的单击光标闪烁。不幸的是,它被放置在该行的开头。您可以通过following the instructions here解决此问题。
这是您的Updated JSFiddle