选中文字,在上方弹出操作按钮;对选中文字进行高亮或者字体等等设置;document.execCommand的使用

  • 效果一
    在这里插入图片描述
  • 效果二
    +在这里插入图片描述
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>contenteditable</title>
  <style>
    #showContent {
      position: absolute;
      top: 0;
      left: 0;
      display: none;
    }

    #showContent button{
      width: 80px;
      height: 30px;
      text-align: center;
      background: #2eabff;
      border: 1px solid #2eabff;
    }

  </style>
</head>
<body>
<button onclick="deleteSty()">删除</button>
<button onclick="insertHorizontalRuleSty()">插入水平线</button>
<button onclick="refresh()">改变</button>
<button onclick="getSelectedText()">getSelectedText</button>

<div contenteditable="true" id="contentEditor">
  我是文本
  这是一段无聊的文字
  <br>
  这是独家度假酒店就

  <p>InsertOrderedList 切换当前选中区是编号列表还是常规格式化块。</p>
  <p></p>
  <p>InsertParagraph 用换行覆盖当前选中区。</p>
  <p>wwwwwwwwwwwwwww</p>
  <p>InsertSelectDropdown 用下拉框控件覆盖当前选中区。</p>
  <p></p>
  <p>InsertSelectListbox 用列表框控件覆盖当前选中区。</p>
  <p></p>
  <p>InsertTextArea 用多行文本输入控件覆盖当前选中区。</p>
</div>

<div id="showContent"><button>share</button></div>

<input type=button value=剪切 οnclick=document.execCommand('Cut')>
<input type=button value=拷贝 οnclick=document.execCommand('Copy')>
<input type=button value=粘贴 οnclick=document.execCommand('Paste')>
<input type=button value=撤消 οnclick=document.execCommand('Undo')>
<input type=button value=重做 οnclick=document.execCommand('Redo') id=button2 name=button2>
<input>
<input type=button value=删除 οnclick=document.execCommand('Delete')>
<input type=button value=黑体 οnclick=document.execCommand('Bold')>
<input type=button value=斜体 οnclick=document.execCommand('Italic')>
<input type=button value=下划线 οnclick=document.execCommand('Underline')>
<input type=button value=停止 οnclick=document.execCommand('stop')>
<input type=button value=保存 οnclick=document.execCommand('SaveAs')>
<input type=button value=另存为 οnclick=document.execCommand('Saveas',false,'c:\\test.htm')>
<input type=button value=字体 οnclick=document.execCommand('FontName',false,fn)>
<input type=button value=字体大小 οnclick=document.execCommand('FontSize',false,fs)>
<input type=button value=刷新 οnclick=document.execCommand('refresh',false,0)>


<script>

  const contentEditor = document.getElementById("contentEditor");
  const showContent = document.getElementById("showContent");
  let startX = 0;
  let startY = 0;


  /*  contentEditor.οnmοusemοve=function(e){
      console.log(e)
    }*/

  contentEditor.onmouseup = function (event) {
    console.log(startX)
    var event = event || window.event;
    // var x = event.clientX;//鼠标的x坐标
    x = startX
    // var y = event.clientY + 10;
    var y = startY;
    var txt;//存文字的变量
    if (window.getSelection) {//获取选中的文字
      txt = window.getSelection().toString();//转换为字符串
    } else {
      txt = document.selection.createRange().text;//IE的写法
    }
    // 判断是否选中了文字
    if (txt) {//所有字符串都为真,空为假,所有数字都为真,0为假
      showBox(x, y, txt);//调用函数
    }
  }


  document.onmousedown = function (event) {
    console.log("onmousedown");
    startX = event.clientX;
    startY = event.clientY-40;
    var event = event || window.event;
    var targetId = event.target ? event.target.id : event.srcElement.id;
    console.log(targetId)
    if (targetId != "contentEditor") {
      showContent.style.display = "none";
    }
  }

  function showBox(mousex, mousey, contentText) {//相关操作
    setTimeout(function () {
        showContent.style.display = "block";
        showContent.style.left = mousex + "px";
        showContent.style.top = mousey + "px";
        // showContent.innerHTML = contentText;
      }
      , 1000)
  }

  function getSelectedText() {
    console.log(window.getSelection().toString())
    console.log(contentEditor)
    const keyword = contentEditor.textContent.substring(contentEditor.selectionStart, contentEditor.selectionEnd);
    console.log(keyword);
  }

  function deleteSty() {
    document.execCommand('delete') //删除选中的部分
  }

  function insertHorizontalRuleSty() {
    document.execCommand('insertHorizontalRule') //在插入点插入一个水平线(删除选中的部分)
  }

  function refresh() {
    debugger
    document.execCommand('bold')
    document.execCommand('copy')
    document.execCommand('fontSize', false, '30px')
  }
</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值