在光标处插入字符(兼容IE&FF)

<html>
<head>

    <script>
        function AddText(str) {
            var ubb = document.getElementById("text1");
            var ubbLength = ubb.value.length;
            ubb.focus();
            if (typeof document.selection != "undefined") {
                document.selection.createRange().text = str;
            }
            else {
                ubb.value = ubb.value.substr(0, ubb.selectionStart) + str + ubb.value.substring(ubb.selectionStart, ubbLength);
            }
        }

 

        function Underline() {
            var tUnderline = prompt("请输入要设置的下划线文字\n标签:[u][/u]", "")//
            if (tUnderline == "" || tUnderline == null) {
                return;
            }
            tUnderline = "[u]" + tUnderline + "[/u]"
            AddText(tUnderline);
        }

        var lastFocusObj;
        function setlastFocusObj(obj) {
            lastFocusObj = obj;
        }

    </script>

</head>
<body>
    <textarea id="text1" cols="40" rows="8"></textarea>
    <a href="javascript:Underline();void(0)">下线线</a>
</body>
</html>

posted @ 2011-06-03 17:52  zzljh  阅读(285)  评论(0编辑  收藏  举报