在 Safari 中单击时如何突出显示输入文本字段? [英] How to highlight input text field upon clicking it in Safari?

查看:46
本文介绍了在 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.

这篇关于在 Safari 中单击时如何突出显示输入文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆