vue項(xiàng)目中在可編輯div光標(biāo)位置插入內(nèi)容的實(shí)現(xiàn)代碼
vue項(xiàng)目中在可編輯div光標(biāo)位置插入內(nèi)容
html:
<div class="mouse-move fl f12 h22 lh22 mg-r5 sms-item" @dragstart="dragStart($event, item.labelName)" draggable='true' v-for="(item, index) in modelCommonList" :key="index" @click="dropRelease($event, item.labelName)"> {{item.labelName}} </div> <div contenteditable="true" @drop="dropRelease($event)" @dragover="allowDrop($event)" ref="smsContent" class="border-r4 f12 h100 pd-tb10 pd-lr10 overflow-scroll-y editable-div" id="smsContent"> </div>
methods:
insertHtmlAtCaret(html) { let sel, range; if (window.getSelection) { // IE9 and non-IE sel = window.getSelection(); if (sel.getRangeAt && sel.rangeCount) { range = sel.getRangeAt(0); range.deleteContents(); let el = document.createElement("div"); el.appendChild(html) var frag = document.createDocumentFragment(), node, lastNode; while ((node = el.firstChild)) { lastNode = frag.appendChild(node); } range.insertNode(frag); if (lastNode) { range = range.cloneRange(); range.setStartAfter(lastNode); range.collapse(true); sel.removeAllRanges(); sel.addRange(range); } } } else if (document.selection && document.selection.type != "Control") { // IE < 9 document.selection.createRange().pasteHTML(html); } }, //開始拖動(dòng)可選字段 dragStart(event, name) { event = event || window.event; this.dragging = name; //str event.dataTransfer.setData(" ", " "); //for firefox }, //阻止默認(rèn)事件 allowDrop(event) { let e = event || window.event; if (e && e.preventDefault) { e.preventDefault(); } else { window.event.returnValue = false; } }, //拖動(dòng)到指定位置并釋放 dropRelease(event, nodeValueName) { event = event || window.event; event.preventDefault(); let textNode = document.createElement('input'); textNode.className = 'mg-lr5 enabledTag'; textNode.type = 'button'; textNode.value = this.dragging || nodeValueName; this.insertHtmlAtCaret(textNode); this.dragging = ''; },
總結(jié)
以上所述是小編給大家介紹的vue項(xiàng)目中在可編輯div光標(biāo)位置插入內(nèi)容的實(shí)現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
vue-cli腳手架config目錄下index.js配置文件的方法
下面小編就為大家分享一篇vue-cli腳手架config目錄下index.js配置文件的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03結(jié)合axios對項(xiàng)目中的api請求進(jìn)行封裝操作
這篇文章主要介紹了結(jié)合axios對項(xiàng)目中的api請求進(jìn)行封裝操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09Avue實(shí)現(xiàn)動(dòng)態(tài)查詢與數(shù)據(jù)展示的示例代碼
Avue是一個(gè)基于Vue.js的前端框架,它是由阿里云開發(fā)的一款企業(yè)級UI組件庫,旨在提供一套全面、易用且高性能的界面解決方案本文介紹了Avue實(shí)現(xiàn)動(dòng)態(tài)查詢與數(shù)據(jù)展示的示例,需要的朋友可以參考下2024-08-08編寫Vue項(xiàng)目,如何給數(shù)組的第一位添加對象數(shù)據(jù)
這篇文章主要介紹了編寫Vue項(xiàng)目,如何給數(shù)組的第一位添加對象數(shù)據(jù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04v-distpicker地區(qū)選擇器組件使用實(shí)例詳解
代碼添加了一個(gè)vDistpickerHandle的事件處理函數(shù)對地區(qū)選擇器中的數(shù)據(jù)進(jìn)行處理,將數(shù)據(jù)存儲(chǔ)到form對象的相應(yīng)屬性中,方便數(shù)據(jù)提交,這篇文章主要介紹了v-distpicker地區(qū)選擇器組件使用,需要的朋友可以參考下2024-02-02