contenteditable="true"的div事件处理

来源:互联网 发布:诺基亚5310xm软件 编辑:程序博客网 时间:2024/05/18 03:25
//页面<div class="test_box" contenteditable="true" id="Monday"></div>

//js

$(".test_box").on('keyup',excute);//禁用鼠标右键document.oncontextmenu=new Function("event.returnValue=false");
/** * 长度控制 */function excute(){    var num1 =$(this)[0].innerText.length;    if(num1 >=100){        layer.tips('长度超出100范围', this);        $(this)[0].innerHTML = $(this)[0].innerText.substring(0,100);        num1 = 100;        po_Last_Div($(this)[0]);    }}//定位div(contenteditable = "true")function po_Last_Div(obj) {    if (window.getSelection) {//ie11 10 9 ff safari        obj.focus(); //解决ff不获取焦点无法定位问题        var range = window.getSelection();//创建range        range.selectAllChildren(obj);//range 选择obj下所有子内容        range.collapseToEnd();//光标移至最后    }    else if (document.selection) {//ie10 9 8 7 6 5        var range = document.selection.createRange();//创建选择对象        //var range = document.body.createTextRange();        range.moveToElementText(obj);//range定位到obj        range.collapse(false);//光标移至最后        range.select();    }}

阅读全文
0 0
原创粉丝点击