選中文字,在上方彈出操作按鈕;對選中文字進行高亮或者字體等等設置;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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章