我怎样才能做到当您在文本区域内单击时,它的整个内容都会被选中?
最终,当您再次单击时,取消选择它。
为了防止用户在每次尝试使用鼠标移动插入符号时选择整个文本时感到恼火,您应该使用focus
事件,而不是click
事件。以下内容将完成这项工作并解决 Chrome 中阻止最简单版本的问题(即仅调用文本区域的select()
方法在一个focus
事件处理程序)停止工作。
js小提琴:http://jsfiddle.net/NM62A/
Code:
<textarea id="foo">Some text</textarea>
<script type="text/javascript">
var textBox = document.getElementById("foo");
textBox.onfocus = function() {
textBox.select();
// Work around Chrome's little problem
textBox.onmouseup = function() {
// Prevent further mouseup intervention
textBox.onmouseup = null;
return false;
};
};
</script>
jQuery 版本:
$("#foo").focus(function() {
var $this = $(this);
$this.select();
// Work around Chrome's little problem
$this.mouseup(function() {
// Prevent further mouseup intervention
$this.unbind("mouseup");
return false;
});
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)