html 富文本编辑器相关--主动选择文字-setSelectionRange-控制光标位置

http://blog.csdn.net/foralienzhou/article/details/52437929

http://www.alixixi.com/web/a/2015070495006.shtml

<!DOCTYPE html>
<html >
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body {
            background-color: #f6f6f6;
        }

        form {
            padding: 30px;
        }

        input {
            display: block;
            margin-top: 10px;
            padding: 10px;
            font-size: 15px;
            color: #333333;
            border: 1px solid #555555;
            border-radius: 5px;
        }
    </style>
</head>
<body>
<form>
    <label>解决chrome中点击input的bug的方案</label>
    <input  placeholder="exception">
    <input  name="money" placeholder="万元">
</form>
</body>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script>
    var tab = false;
    document.addEventListener('keydown', function(e) {
        if (e.keyCode == 9) {
            tab = true;
        }
    });
    document.getElementById('exception').addEventListener('focus', function() {
        tab = false;
    });
    document.getElementById('money').addEventListener('click', function() {
        tab = true;
        this.focus();
    });

    document.getElementById('money').addEventListener('focus', function() {
        if (tab) {
            var val = this.value,
                len = val.length;
            if (val.indexOf('万元') !== -1) {
                pos = len - 2;
                setTimeout(function() {
                    changeCursorPos('money', pos);
                }, 0);
            } else {
                $(this).val(val + '万元');
                pos = len;
                setTimeout(function() {
                    changeCursorPos('money', pos);
                }, 0);
            }
        } else {
            this.blur();
        }
        tab = false;
    });

    function changeCursorPos(inputId, pos) {
        var inpObj = document.getElementById(inputId);
        if (inpObj.setSelectionRange) {
            inpObj.setSelectionRange(pos, pos);
        } else {
            console.log('不兼容该方法');
        }
    }
</script>
</html>