zoukankan      html  css  js  c++  java
  • div里粘贴文字后,移动光标至最后

    cursormanager.js

    //Namespace management idea from http://enterprisejquery.com/2010/10/how-good-c-habits-can-encourage-bad-javascript-habits-part-1/
    (function( cursorManager ) {

    //From: http://www.w3.org/TR/html-markup/syntax.html#syntax-elements
    var voidNodeTags = ['AREA', 'BASE', 'BR', 'COL', 'EMBED', 'HR', 'IMG', 'INPUT', 'KEYGEN', 'LINK', 'MENUITEM', 'META', 'PARAM', 'SOURCE', 'TRACK', 'WBR', 'BASEFONT', 'BGSOUND', 'FRAME', 'ISINDEX'];

    //From: http://stackoverflow.com/questions/237104/array-containsobj-in-javascript
    Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
    if (this[i] === obj) {
    return true;
    }
    }
    return false;
    }

    //Basic idea from: http://stackoverflow.com/questions/19790442/test-if-an-element-can-contain-text
    function canContainText(node) {
    if(node.nodeType == 1) { //is an element node
    return !voidNodeTags.contains(node.nodeName);
    } else { //is not an element node
    return false;
    }
    };

    function getLastChildElement(el){
    var lc = el.lastChild;
    while(lc && lc.nodeType != 1) {
    if(lc.previousSibling)
    lc = lc.previousSibling;
    else
    break;
    }
    return lc;
    }

    //Based on Nico Burns's answer
    cursorManager.setEndOfContenteditable = function(contentEditableElement)
    {

    while(getLastChildElement(contentEditableElement) &&
    canContainText(getLastChildElement(contentEditableElement))) {
    contentEditableElement = getLastChildElement(contentEditableElement);
    }

    var range,selection;
    if(document.createRange)//Firefox, Chrome, Opera, Safari, IE 9+
    {
    range = document.createRange();//Create a range (a range is a like the selection but invisible)
    range.selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range
    range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
    selection = window.getSelection();//get the selection object (allows you to change selection)
    selection.removeAllRanges();//remove any selections already made
    selection.addRange(range);//make the range you have just created the visible selection
    }
    else if(document.selection)//IE 8 and lower
    {
    range = document.body.createTextRange();//Create a range (a range is a like the selection but invisible)
    range.moveToElementText(contentEditableElement);//Select the entire contents of the element with the range
    range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
    range.select();//Select the range (make it the visible selection
    }
    }

    }( window.cursorManager = window.cursorManager || {}));




    <div contenteditable="true" class="text" id="content" v-on:keyup="triggerKeyup" v-on:paste="triggerPaste">




    triggerPaste:function(e) {
    if (plus.os.name == "iOS") {
    var clipboardData, pastedData;

    // Stop data actually being pasted into div
    e.stopPropagation();
    e.preventDefault();

    // Get pasted data via clipboard API
    clipboardData = e.clipboardData || window.clipboardData;
    pastedData = clipboardData.getData('Text');
    //rDataHTML = clipboardData.getData('text/html');
    //rDataPText = clipboardData.getData('text/plain');

    document.getElementById('content').innerHTML = document.getElementById('content').innerHTML + pastedData;

    cursorManager.setEndOfContenteditable(document.getElementById('content'));
    }
    },
  • 相关阅读:
    通用js模块02:validutils.js
    通用js模块04:cookieUtils.js
    通用js模块03:formatutils.js
    通用js模块01:stringutils.js
    应用开发平台与代码生成工具感想
    linq to sql 中isnumeric的使用
    很惭愧啊
    错误:”未能加载文件或程序集“System.Web.Mvc, Version=2.0.0.0” 解决方法
    今天又温习了一下磁盘阵列的概念
    ashx的说明
  • 原文地址:https://www.cnblogs.com/yuanxiaoping_21cn_com/p/5691741.html
Copyright © 2011-2022 走看看