首页 > 其他分享 >jquery实现简单富文本编辑

jquery实现简单富文本编辑

时间:2022-11-08 14:48:33浏览次数:39  
标签:jquery 文本编辑 return dom style styleList sec 简单 var

 

 

<script>

$("#under").click(function () {
var sec = getSelection()
if (sec == undefined) {
return;
}
ReplaceInnerHtml(sec.selectStr, GetSelectedStyle(sec.dom,'text-decoration', 'underline'));
})
$("#bold").click(function () {
var sec = getSelection();
if (sec == undefined) {
return;
}
ReplaceInnerHtml(sec.selectStr, GetSelectedStyle(sec.dom,'font-weight','bold'));
})
$("#big").click(function () {
var sec = getSelection()
if (sec == undefined) {
return;
}
ReplaceInnerHtml(sec.selectStr, GetSelectedStyle(sec.dom,'font-size','18px'));
})
$("#small").click(function () {
var sec = getSelection()
if (sec == undefined) {
return;
}
ReplaceInnerHtml(sec.selectStr, GetSelectedStyle(sec.dom,'font-size','14px'));
})
function getSelection() {
var selectStr = window.getSelection().toString();
if (selectStr == '')
return undefined;
var dom = window.getSelection().getRangeAt(0).commonAncestorContainer;
if ($("#content").find(dom).length == 0) {
return undefined;
}

return { selectStr: selectStr,dom:dom}
}
function GetSelectedStyle(dom,key,value) {
var style = '';
if ($(dom).prop('nodeName') == 'SPAN') {
style = $(dom).attr('style');
}
else if ($(dom).parent().prop('nodeName') == 'SPAN') {
style = $(dom).parent().attr('style');
}
if (style == undefined) {
style = '';
}
style = style.replace(' ', '');
if (key == '' || key == undefined) {
return style;
}
var styleList = style.split(';');
var item = styleList.find(a => a.indexOf(key) > -1)
if (item != undefined) {
for (var i = 0; i < styleList.length; i++) {
if (styleList[i].indexOf(key) > -1) {
if (value == '') {
styleList[i] = '';
}
else {
styleList[i] = key + ':' + value;
}
}
}

}
else {
styleList.push(key + ':' + value)
}

styleList = styleList.filter(a => a != '');
styleList.push('');
return styleList.join(';');
}
function ReplaceInnerHtml(html, style) {
document.execCommand('insertHTML', 'false', '<span id="' + id + '" style="' + style + '">' + html + '</span>');

const range = window.getSelection().getRangeAt(0)
const dom = document.getElementById(id)
range.setStart(dom, 0)
range.setEnd(dom, 1)
id++;
}

</script>

<div style="width:200px;height:200px;margin:auto" class="body">

<button id="under">下划线</button>
<button id="bold">加粗</button>
<button id="big">大</button>
<button id="small">小</button>
<select id="size">
<option>9px</option>
<option>14px</option>
<option>18px</option>
</select>
<div id="content">
<div class="text" contenteditable="true">
65666666666666666666666666
</div>
</div>
</div>

标签:jquery,文本编辑,return,dom,style,styleList,sec,简单,var
From: https://www.cnblogs.com/QJZY/p/16869639.html

相关文章

  • Nexus搭建maven仓库并简单使用
    一、基本介绍参考:https://www.hangge.com/blog/cache/detail_2844.html1、为什么搭建私服如果没有私服,需要的构件都需要通过maven的中央仓库或者第三方的maven仓库下载......
  • javaScript简单的赋值运算符
    <----------------------------------------------赋值运算符------------------------------------------------------------>=号是赋值操作+=是加等于号,和(n=n+1)是......
  • 定制富文本编辑器
    定制富文本编辑器很多时候<textarea>并不能满足我们对文本输入的需求,当我们需要为输入的文本添加格式时,我们需要使用像quill这样的富文本编辑器来完成富文本的输入。本文将......
  • 【Python零基础入门篇 · 35】:协程和IO操作的简单理解
    协程和IO操作的简单理解协程的理解协程,又称微线程,纤程。英文名Coroutine。协程是python个中另外一种实现多任务的方式,只不过比线程更小占用更小执行单元(理解为需要的资......
  • js简单时分秒计时器
    <divstyle="text-align:center"><inputtype="text"id="timetext"value="00时00分00秒"readonly><br><spanid="_h">00时</span><spanid="_m">00分<......
  • 中央处理器__ 时序产生器和控制方式(简单看)
    时序信号的作用和体制CPU中也有一个类似“作息时间”的东西,称为时序信号。计算机所以能够准确、迅速、有条不紊的工作,正是因为CPU中有一个时序信号发生器。作用:CPU中......
  • Git简单使用
    1.下载安装Git https://git-scm.com2.配置本地Git桌面右键gitbash#配置用户名gitconfig--globaluser.name"username"  //(名字)#配置邮箱gitconfig--glo......
  • jq(JQuery)操作cookie
     先引入jq封装好的方法<scriptsrc="//cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script> 设置新的cookie$.cookie('name','yvioo');//设......
  • 简单的反射增删改查
    #coding:utf-8importosimportsysimportjsonroot_dir=os.path.dirname(os.path.dirname(__file__))sys.path.append(root_dir)db_dir=os.path.join(root_dir,'db')......
  • Python条件语句和循环语句简单使用方法
    1.Python条件语句Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。可以通过下图来简单了解条件语句的执行过程:Python程序语言指定任......