富文本编辑器中,如何获取光标所在的元素?

富文本编辑器用的是nicEditor,编辑区域中有很多元素。如图,如何获取‘hi’所在节点?


https://img4.mukewang.com/5c49759100013c7408000421.jpg

倚天杖
浏览 1848回答 1
1回答

慕娘9325324

类似于fontSize的命令,添加face=id的font元素,再通过此id定位到当前元素:var id="execCommandTest"document.execCommand('fontName',null,id)$(elem).find("font[face='"+id+"']")&nbsp;document.execCommand('undo',null)//取到元素后去除标记,简直毫无痕迹demo:<div contenteditable="true">&nbsp; &nbsp; &nbsp; &nbsp; 44444444&nbsp; &nbsp; </div>&nbsp; &nbsp; <button type="button" >click</button>&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; $("button").click(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var id="execCommandTest"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.execCommand('fontName',null,id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log($('div').find("font[face='"+id+"']").html())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.execCommand('undo',null)&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; </script>&nbsp;&nbsp;效果:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript