我在Select all content of textbox when it received focus (JavaScript or jQuery)看到了脚本。
不幸的是,在尝试为 IE10 实现它时,我得出的结论是,焦点也会在稍后被清除,并且防止默认(适用于基于 WebKit 的浏览器)似乎根本不起作用。
我设法让它在 IE10 中正常运行,但额外的布尔变量感觉有点脏。
(基本)html:
<div id="ContentDiv">
    <input type="text" value="default" />
</div>
代码:
$(document).ready(initialize);
function initialize() {
    var tmp;
    $("#ContentDiv").on({
        focus: function (e) {
            //select for all browsers
            $(this).select();
            tmp = true;
        },
        mouseup: function (e) {
            //reselect for IE10
            if (tmp) {
                this.select();
                tmp = false;
            }
            //chrome still needs this
            e.preventDefault();
        }
    }, "input:text");
}
示例:jsfiddle
我的问题:有没有人知道解决这个焦点选择问题的更清洁的方法?