在 Safari 中单击时如何突出显示输入文本字段?

在 Safari 中单击时如何突出显示输入文本字段?

问题描述:

我希望在获得焦点时突出显示输入文本框的值,无论是单击它还是使用 Tab 键.

I would like the value of the input text box to be highlighted when it gains focus, either by clicking it or tabbing to it.

<html>
<body>

<script>
function focusTest(el)
{
   el.select();
}
</script>

<input type="text" value="one" OnFocus="focusTest(this); return false;" />
<br/>
<input type="text" value="two" OnFocus="focusTest(this); return false;" />

</body>
</html>

在 Firefox 或 IE 中单击任一输入字段时,该字段会突出显示.但是,这在 Safari 中不起作用.(注意:它在字段之间切换时有效.)

When either input field is clicked in Firefox or IE, that field is highlighted. However, this doesn't work in Safari. (NOTE: it works when tabbing between fields.)

我注意到 Safari 实际上是在选择文本然后快速删除选择.

I noticed Safari is actually selecting the text then removing the selection quickly.

所以我尝试了这个适用于所有浏览器的快速解决方法:

So I tried this quick workaround that works in all browsers:

function focusTest(el)
{
  setTimeout (function () {el.select();} , 50 );
}


进一步测试后发现 OnMouseUp 事件正在清除选择,因此足以添加

Edit :
Upon further testing it turns out the OnMouseUp event is clearing the selection so it is enough to add

onMouseUp="return false;"

到输入元素,使事情按其应有的方式工作.

to the input element for things to work as they should.