zoukankan      html  css  js  c++  java
  • 【表单】四

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    
    <body>
    <textarea id="text">这些是默认文字</textarea>
    <input type="button" onclick="insertText(document.getElementById('text'), ' 新文字&mdash;YoyiorLee ')" value="插入文字" />
    <input type="button" onclick="moveEnd(document.getElementById('text'))" value="移到末尾" />
    <script>
    function insertText (obj, str) {
        obj.focus();
        // 只ie认document.selection
        if (document.selection) {
            // 替换选中文本,光标处插入,插入后光标停留在了原位置
            var sel = document.selection.createRange();
            sel.text = str;
        } else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
            // 不选中文本,只是光标位置,选中文本,选中文本的起始跟结束位置
            alert(obj.selectionStart + '' + obj.selectionEnd);
            var startPos = obj.selectionStart,
                endPos = obj.selectionEnd,
                cursorPos = startPos,
                tmpStr = obj.value;
            obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
            cursorPos += str.length;
            obj.selectionStart = obj.selectionEnd = cursorPos;
        } else {
            obj.value += str;
        }
    }
    
    function moveEnd (obj) {
        obj.focus();
        var len = obj.value.length;
        if (document.selection) {
            // javascript moveStart和moveEnd方法 http://hi.baidu.com/mascotdai/blog/item/2538030e979e56c27bcbe1f5.html
            var sel = obj.createTextRange();
            // 更改范围的开始位置;将开始点向后移动len个字符
            sel.moveStart('character', len);
            // 将插入点移动到当前范围的开始或结尾;可选参数是Boolean值,它指出范围是在当前范围的开始点重合,还是结束点重合。默认值为true,在开始点重合
            sel.collapse();
            // 将当前选择区设置为当前对象;选中当前文本范围内的文本,显示光标也必须利用它来实现,因为所谓的光标可以理解为边界重合的范围
            sel.select();
        } else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
            obj.selectionStart = obj.selectionEnd = len;
        }
    }
    </script>
    </body>
    </html>
  • 相关阅读:
    Android学习笔记事件处理机制 希
    DIY我的博客皮肤 希
    CSS学习小札居中问题及解决方案 希
    Entity Framework 教程
    springcahce集成redis 设置过期时间 Hiro
    springboot集成springcache Hiro
    Geotools核心特点以及支持数据的格式和标准
    github使用
    一个jekyll使用大牛的博客
    在没有root权限情况下安装python
  • 原文地址:https://www.cnblogs.com/jzm17173/p/2618195.html
Copyright © 2011-2022 走看看