我会飞

时间:2017-10-17 22:23:37来源:杰瑞文章网点击:作文字数:500字
RT,或许你们脑子里首先想到的是使用富文本编辑器,那么恭喜你,想法是对的。But,很多富文本编辑器并没有提供很多的api,这里推荐使用百度Ueditor,它提供了很好的api以及文档,但是话又说回来,如果使用这个,有点大材小用了。 那么,说回正题,如题,如何使用简单的代码实现这一需求呢,话不多说,老规矩,直接上代码: //html代码: //js代码: $('#textarea').on('select',function () { message.setCaret(this); }).on('click',function () { message.setCaret(this); }).on('keyup',function () { message.setCaret(this); }); $('#insert').on('click',function () { var textareaStr = $('#textarea').val(); message.insertAtCaret($('#textarea')[0],'<--需要插入的文本-->'); }); var message = { setCaret :function (textObj) { if (textObj.createTextRange) { textObj.caretPos = document.selection.createRange().duplicate(); } } insertAtCaret :function (textObj, textFeildValue) { if (document.all) { if (textObj.createTextRange && textObj.caretPos) { var caretPos = textObj.caretPos; caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? textFeildValue + ' ' : textFeildValue; } else { textObj.value = textFeildValue; } } else { if (textObj.setSelectionRange) { var rangeStart = textObj.selectionStart; var rangeEnd = textObj.selectionEnd; var tempStr1 = textObj.value.substring(0, rangeStart); var tempStr2 = textObj.value.substring(rangeEnd); textObj.value = tempStr1 + textFeildValue + tempStr2; } else { alert("This version of Mozilla based browser does not support setSelectionRange"); } } } OK,直接复制粘贴试试吧 =0=
作文投稿

我会飞一文由杰瑞文章网免费提供,本站为公益性作文网站,此作文为网上收集或网友提供,版权归原作者所有,如果侵犯了您的权益,请及时与我们联系,我们会立即删除!

杰瑞文章网友情提示:请不要直接抄作文用来交作业。你可以学习、借鉴、期待你写出更好的作文。

我会飞相关的作文:

说说你对这篇作文的看法吧

最新发表的关于快乐的作文

    SQL Error: select id,classid,ttid,onclick,plnum,totaldown,newspath,filename,userid,username,firsttitle,isgood,ispic,istop,isqf,ismember,isurl,truetime,lastdotime,havehtml,groupid,userfen,titlefont,titleurl,stb,fstb,restb,keyboard,eckuid,title,ftitle,newstime,titlepic,smalltext,writer,diggtop from ***_ecms_news where (classid='269') order by newstime desc limit 10

最受欢迎的关于快乐的作文

    SQL Error: select id,classid,ttid,onclick,plnum,totaldown,newspath,filename,userid,username,firsttitle,isgood,ispic,istop,isqf,ismember,isurl,truetime,lastdotime,havehtml,groupid,userfen,titlefont,titleurl,stb,fstb,restb,keyboard,eckuid,title,ftitle,newstime,titlepic,smalltext,writer,diggtop from ***_ecms_news where (classid='269') order by onclick desc limit 10