JS文字操作庫(親測可用)

使用

<template><div class="app"><li class="main_right-btn" @click="selectionColor('yellow')">題干標識</li><li class="main_right-btn" @click="selectionColor('transparent')">取消標識</li><li class="main_right-btn">撤銷</li><li class="main_right-btn">恢復</li><li class="main_right-btn" @click="copyText">復制</li><li class="main_right-btn" @click="cutstrText">剪切</li><li class="main_right-btn" @click="pasteText">粘貼</li><textarea id="examText"></textarea></div>
</template><script>
import Mark from '@/utils/TextMark'
export default {components: {},data() {return {}},mounted() {},computed: {},methods: {// 剪切文字方法cutstrText() {this.copyVal = Mark.cutstr('examText')console.log('    this.copyVal ',    this.copyVal );},// 粘貼文字方法pasteText() {Mark.parsestr('examText', this.copyVal)},// 復制當前文字方法copyText() {this.copyVal = Mark.getSelectedText()?.trim()},// 標識文字方法selectionColor(color) {Mark.ColorizeSelection(color)},}
}
</script>
<style scoped  lang="less"></style>

庫內容

const Mark = {// 標記方法 SGetNextLeaf: function (node) {while (!node.nextSibling) {node = node.parentNodeif (!node) {return node}}var leaf = node.nextSiblingwhile (leaf.firstChild) {leaf = leaf.firstChild}return leaf},GetPreviousLeaf: function (node) {while (!node.previousSibling) {node = node.parentNodeif (!node) {return node}}var leaf = node.previousSiblingwhile (leaf.lastChild) {leaf = leaf.lastChild}return leaf},// If the text content of an element contains white-spaces only, then does not need to colorizeIsTextVisible: function (text) {for (var i = 0; i < text.length; i++) {if (text[i] != ' ' && text[i] != '\t' && text[i] != '\r' && text[i] != '\n') return true}return false},ColorizeLeaf: function (node, color) {if (!Mark.IsTextVisible(node.textContent)) returnvar parentNode = node.parentNodeif (!parentNode) return// if the node does not have siblings and the parent is a span element, then modify its colorif (!node.previousSibling && !node.nextSibling) {if (parentNode.tagName.toLowerCase() == 'span') {parentNode.style.backgroundColor = colorreturn}}// Create a span element around the nodevar span = document.createElement('span')span.style.backgroundColor = colorvar nextSibling = node.nextSiblingparentNode.removeChild(node)span.appendChild(node)parentNode.insertBefore(span, nextSibling)},ColorizeLeafFromTo: function (node, color, from, to) {var text = node.textContentif (!Mark.IsTextVisible(text)) returnif (from < 0) from = 0if (to < 0) to = text.lengthif (from == 0 && to >= text.length) {// to avoid unnecessary span elementsMark.ColorizeLeaf(node, color)return}var part1 = text.substring(0, from)var part2 = text.substring(from, to)var part3 = text.substring(to, text.length)var parentNode = node.parentNodevar nextSibling = node.nextSiblingparentNode.removeChild(node)if (part1.length > 0) {var textNode = document.createTextNode(part1)parentNode.insertBefore(textNode, nextSibling)}if (part2.length > 0) {var span = document.createElement('span')span.style.backgroundColor = colorvar textNode = document.createTextNode(part2)span.appendChild(textNode)parentNode.insertBefore(span, nextSibling)}if (part3.length > 0) {var textNode = document.createTextNode(part3)parentNode.insertBefore(textNode, nextSibling)}},ColorizeNode: function (node, color) {var childNode = node.firstChildif (!childNode) {Mark.ColorizeLeaf(node, color)return}while (childNode) {// store the next sibling of the childNode, because colorizing modifies the DOM structurevar nextSibling = childNode.nextSiblingMark.ColorizeNode(childNode, color)childNode = nextSibling}},ColorizeNodeFromTo: function (node, color, from, to) {var childNode = node.firstChildif (!childNode) {Mark.ColorizeLeafFromTo(node, color, from, to)return}for (var i = from; i < to; i++) {Mark.ColorizeNode(node.childNodes[i], color)}},ColorizeSelection: function (color) {if (!!window.ActiveXObject || 'ActiveXObject' in window) {document.execCommand('BackColor', false, color)return}// all browsers, except IE before version 9if (window.getSelection) {var selectionRange = window.getSelection()if (selectionRange.isCollapsed) {return}var range = selectionRange.getRangeAt(0)// store the start and end points of the current selection, because the selection will be removedvar startContainer = range.startContainervar startOffset = range.startOffsetvar endContainer = range.endContainervar endOffset = range.endOffset// because of Opera, we need to remove the selection before modifying the DOM hierarchyselectionRange.removeAllRanges()if (startContainer == endContainer) {//同一個節點時,直接標記顏色Mark.ColorizeNodeFromTo(startContainer, color, startOffset, endOffset)} else {if (startContainer.firstChild) {var startLeaf = startContainer.childNodes[startOffset]} else {//標記第一段節點var startLeaf = Mark.GetNextLeaf(startContainer)Mark.ColorizeLeafFromTo(startContainer, color, startOffset, -1)}if (endContainer.firstChild) {if (endOffset > 0) {var endLeaf = endContainer.childNodes[endOffset - 1]} else {var endLeaf = Mark.GetPreviousLeaf(endContainer)}} else {var endLeaf = Mark.GetPreviousLeaf(endContainer)Mark.ColorizeLeafFromTo(endContainer, color, 0, endOffset)}while (startLeaf) {var nextLeaf = Mark.GetNextLeaf(startLeaf)Mark.ColorizeLeaf(startLeaf, color)if (startLeaf == endLeaf) {break}startLeaf = nextLeaf}}} else {// Internet Explorer before version 9document.execCommand('BackColor', false, color)}},// 標記方法 E// 粘貼方法parsestr(dom, myValue) {if (!myValue) returnlet myField = document.getElementById(dom)//IE supportif (document.selection) {myField.focus()sel = document.selection.createRange()sel.text = myValuesel.select()}//MOZILLA/NETSCAPE supportelse if (myField.selectionStart || myField.selectionStart == '0') {var startPos = myField.selectionStartvar endPos = myField.selectionEndvar restoreTop = myField.scrollTopmyField.value =myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length)if (restoreTop > 0) {myField.scrollTop = restoreTop}myField.focus()myField.selectionStart = startPos + myValue.lengthmyField.selectionEnd = startPos + myValue.length} else {myField.value += myValuemyField.focus()}},// 復制方法getSelectedText() {return window.getSelection().toString()},// 剪切方法cutstr(id){// 獲取Textarea元素var textarea = document.getElementById(id);// 獲取選中文本的起始和結束位置var selectionStart = textarea.selectionStart;var selectionEnd = textarea.selectionEnd;// 提取選中的文本var selectedText = textarea.value.substring(selectionStart, selectionEnd);// 刪除選中的文本textarea.value = textarea.value.substring(0, selectionStart) + textarea.value.substring(selectionEnd);// 將選中的文本剪切到剪貼板document.addEventListener('cut', function(e) {// 將光標移到Textarea內部textarea.focus();e.clipboardData.setData('text/plain', selectedText);e.preventDefault();});// 執行剪切操作document.execCommand('cut');return selectedText;}
}
export default Mark

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/163936.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/163936.shtml
英文地址,請注明出處:http://en.pswp.cn/news/163936.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

K-means算法

K-means算法 Lloyd k-means Algorithm 樣本矩陣&#xff1a; X [ x 1 , x 2 , . . . , x n ] ∈ R d n X[x_1,x_2,...,x_n] ∈R^{dn} X[x1?,x2?,...,xn?]∈Rdn&#xff0c;有n個 x i x_i xi?每個 x i x_i xi?是d維 簇集合&#xff1a; C [ C 1 , C 2 , . . . , C c …

自由飛翔之小鳥

一、創建文件、包、類、插入圖片文件 二、app包 1、Gameapp類&#xff08;運行游戲&#xff09; package app;import main.GameFrame;public class Gameapp {public static void main(String[] args) {//游戲的入口new GameFrame();} } 三、main包 1、Barrier&#xff08;障…

【實驗】配置用戶自動獲取IPv6地址的案例

【贈送】IT技術視頻教程&#xff0c;白拿不謝&#xff01;思科、華為、紅帽、數據庫、云計算等等?編輯https://xmws-it.blog.csdn.net/article/details/117297837?spm1001.2014.3001.5502https://xmws-it.blog.csdn.net/article/details/117297837?spm1001.2014.3001.5502【…

Pyqt5 設置保存上一次結果(配置文件)

效果 每次打開Pyqt5打包后的程序&#xff0c;默認顯示的是上一次的結果 例如下圖的 文件路徑、表名、類型等 大致的思路 Pyqt5自帶的方法QSettings實現保存上一次的設置&#xff0c;其思路是讀取ini文件&#xff0c;如果不存在就是程序的初始狀態&#xff0c;如果存在則可以讀取…

C++程序中dump文件生成方法詳解

最近項目中新作成了一個動態鏈接庫&#xff0c;長時間運行后&#xff0c;偶爾會崩潰。根據log分析&#xff0c;被調用的動態庫函數最外層catch到了這個異常&#xff0c;但是不能定位哪里出了問題。另外雖然上層exe是有dump文件輸出處理的&#xff0c;但是在C中&#xff0c;如果…

如何利用Python進行數據歸一化?

1. 知識簡介 數據歸一化是數據預處理的一項重要步驟&#xff0c;它對于提高模型性能、加速模型訓練、避免數值計算問題以及提高模型的泛化能力都具有重要作用。進行數據歸一化可以起到以下作用&#xff1a;消除量綱影響&#xff0c;加速模型收斂&#xff0c;提高模型性能&…

硅谷大寬服務器:引領互聯網新時代的核心技術

在當今這個信息爆炸的時代&#xff0c;數據已經成為了企業和個人的重要資產。服務器作為數據的存儲和處理中心&#xff0c;其重要性不言而喻。硅谷大寬服務器以其卓越的性能、穩定的運行和優質的服務&#xff0c;贏得了全球眾多企業和個人的信賴和選擇。 硅谷大寬服務器的特點…

圖解分庫分表

中大型項目中&#xff0c;一旦遇到數據量比較大&#xff0c;小伙伴應該都知道就應該對數據進行拆分了。有垂直和水平兩種。 垂直拆分比較簡單&#xff0c;也就是本來一個數據庫&#xff0c;數據量大之后&#xff0c;從業務角度進行拆分多個庫。如下圖&#xff0c;獨立的拆分出…

Redisson分布式鎖實現原理

Redisson主要解決一下問題 重入問題&#xff1a;重入問題是指 獲得鎖的線程可以再次進入到相同的鎖的代碼塊中&#xff0c;可重入鎖的意義在于防止死鎖&#xff0c;比如HashTable這樣的代碼中&#xff0c;他的方法都是使用synchronized修飾的&#xff0c;假如他在一個方法內&a…

解決Spring Boot應用在Kubernetes上健康檢查接口返回OUT_OF_SERVICE的問題

現象 在將Spring Boot應用部署到Kubernetes上時&#xff0c;健康檢查接口/healthcheck返回的狀態為{"status":"OUT_OF_SERVICE","groups":["liveness","readiness"]}&#xff0c;而期望的是返回正常的健康狀態。值得注意的…

VTK物體表面畫貼合線條

1、自由畫線 2、曲線擬合畫線 3、三點閉合曲線

Docker Compose部署微服務項目實戰講解

一、Docker Compose簡介 當需要在多個容器之間協調和管理應用程序時&#xff0c;Docker Compose是一個非常有用的工具。它允許通過一個配置文件來定義、配置和啟動多個 Docker 容器&#xff0c;使得整個應用程序的部署變得更加簡單和一致。以下是 Docker Compose 的一些重要概…

Linux使用寶塔面板+Discuz+cpolar內網穿透工具搭建可公網訪問論壇

Linux寶塔面板搭建Discuz論壇&#xff0c; 并內網穿透實現公網訪問 文章目錄 Linux寶塔面板搭建Discuz論壇&#xff0c; 并內網穿透實現公網訪問前言1.安裝基礎環境2.一鍵部署Discuz3.安裝cpolar工具4.配置域名訪問Discuz5.固定域名公網地址6.配置Discuz論壇 前言 Crossday Di…

【 圖片加載】Vue前端各種圖片引用

文章目錄 一、圖片作為js常量&#xff08;常作為配置項的值 &#xff09;1、在線鏈接2、本地圖片 二、圖片img標簽1、一般的src2、動態的src用require3、src可以接收二進制文件blob&#xff08;如后端返回的、a-upload傳的圖片) 三、背景圖片 一、圖片作為js常量&#xff08;常…

8款那些年救過我的數據文件恢復軟件 - 誤刪除重要文件的“后悔藥”

無論您在保存備份方面多么小心&#xff0c;災難有時還是會發生。有時您的備份軟件無法按預期運行。 如果您的外部驅動器靠近您的設備&#xff0c;發生火災/洪水/故障時&#xff0c;有時備份會與原始文件一起丟失。即使是云存儲也不能避免故障。 還有一個事實是&#xff0c;我…

C++設計模式之工廠模式(上)——簡單工廠模式

工廠模式 概述簡單工廠模式介紹示例示例使用運行結果缺點 概述 工廠模式屬于一種創建型設計模式。其可以分為簡單工廠模式&#xff0c;工廠模式和抽象工廠模式。工廠模式分為上、中、下三篇&#xff0c;本篇主要介紹簡單工廠模式。 簡單工廠模式 介紹 簡單工廠模式可以理解…

二叉樹題目:具有所有最深結點的最小子樹

文章目錄 題目標題和出處難度題目描述要求示例數據范圍 解法一思路和算法代碼復雜度分析 解法二思路和算法代碼復雜度分析 題目 標題和出處 標題&#xff1a;具有所有最深結點的最小子樹 出處&#xff1a;865. 具有所有最深結點的最小子樹 難度 5 級 題目描述 要求 給定…

HCIP-六、OSPF-2 綜合實驗

六、OSPF-2 綜合實驗 實驗拓撲實驗需求及解法1.設備名稱和部分IP地址已配置2.所有設備運行OSPF&#xff0c;進程號為13.區域間路由匯總4.外部路由匯總5.下發默認路由6. 虛鏈路 實驗拓撲 實驗需求及解法 本實驗模擬OSPF綜合型網絡&#xff0c;按照以下需求完成實驗。 1.設備名…

EventLog Analyzer:強大的日志管理與分析工具

隨著企業網絡規模的擴大和信息系統的復雜化&#xff0c;安全日志管理和分析成為了至關重要的一環。在這個背景下&#xff0c;EventLog Analyzer嶄露頭角&#xff0c;成為一款備受推崇的日志管理與分析工具。本文將介紹EventLog Analyzer的主要特點、功能以及為企業帶來的實際價…