美文网首页
关于div标签可编辑且插入其它标签的问题

关于div标签可编辑且插入其它标签的问题

作者: 半笑半醉間 | 来源:发表于2019-12-17 18:00 被阅读0次

落花有意随流水,流水无心蝶恋花

最近在做一个类似下面的功能:

1576576638695.jpg
由于textarea不能满足该要求,所以只有通过div来实现,由于时间关系就不在这里啰嗦了,直接上代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <%@include file="/common/common.jsp" %>
    <style type="text/css">
        h3{margin-bottom:10px;margin-top: 20px;}
        .tip{color:grey;font-style: italic;margin-left: 20px;}
    </style>
</head>
<body>
<div class="page">
    <div class="page-content">
        <h3>标准按钮</h3>
        <button class="ui-button ui-button-primary mr-2">按钮</button>
        <button class="ui-button ui-button-success mr-2">按钮</button>
        <button class="ui-button ui-button-warning mr-2">按钮</button>
        <button id="sendEmoji" class="ui-button ui-button-error mr-2">添加标签按钮</button>
    </div>

    <div id="editor"  placeholder="请输入您的内容" contenteditable="true"
         style="overflow-y:scroll;width: 500px;height: 300px;margin-left: 100px; line-height: 20px">

    </div>
</div>
<script type="text/javascript">

    var inputResizeDom;
    var edit = document.getElementById('editor')
    var sendEmoji = document.getElementById('sendEmoji')

    // 定义最后光标对象
    var lastEditRange;

    // 编辑框点击事件
    edit.onclick = function() {
        // 获取选定对象
        var selection = getSelection()
        // 设置最后光标对象
        lastEditRange = selection.getRangeAt(0)
    }

    // 编辑框按键弹起事件
    edit.onkeyup = function() {
        // 获取选定对象
        var selection = getSelection()
        // 设置最后光标对象
        lastEditRange = selection.getRangeAt(0)
    }

/* 此处代码可以屏蔽掉,这是每次插入后定位到末尾的代码
    editor.onfocus = function () {
        window.setTimeout(function () {
            var sel,range;
            if (window.getSelection && document.createRange) {
                range = document.createRange();
                range.selectNodeContents(editor);
                range.collapse(true);
                range.setEnd(editor, editor.childNodes.length);
                range.setStart(editor, editor.childNodes.length);
                sel = window.getSelection();
                sel.removeAllRanges();
                sel.addRange(range);
            } else if (document.body.createTextRange) {
                range = document.body.createTextRange();
                range.moveToElementText(editor);
                range.collapse(true);
                range.select();
            }
        }, 1);
    }
*/
    // 表情点击事件
    sendEmoji.onclick = function() {
        insertAtCursorDiv(edit,"添加标签按钮");
    }

    //插入标签
    function insertAtCursorDiv(myField, myValue) {
        // 编辑框设置焦点
        myField.focus()
        // 获取选定对象
        var selection = window.getSelection();//getSelection()
        // 判断是否有最后光标对象存在
        if (lastEditRange) {
            // 存在最后光标对象,选定对象清除所有光标并添加最后光标还原之前的状态
            selection.removeAllRanges()
            selection.addRange(lastEditRange)
        }

        var emojiText = document.createElement("input")
        emojiText.setAttribute("value", "标签的文字");
        emojiText.setAttribute("data-code", "123");
        emojiText.setAttribute("disabled", 'true');
        emojiText.style.width = input_resize("标签的文字") + 'px';
        emojiText.style.background = 'lightskyblue';
        emojiText.style.borderRadius = '10px';
        emojiText.style.textAlignLast = 'center';
        emojiText.style.font = '16px';

        insertHtmlAtCaret(emojiText.outerHTML);
        // 无论如何都要记录最后光标对象
        lastEditRange = selection.getRangeAt(0)
    }

    var modalVm = avalon.define({
        $id:'inputLabel',

    });

    componentDidMount();

    function componentDidMount(){
        //增加隐藏节点,编辑模板时计算标签宽度
        if($('.hidden').length < 1){
            inputResizeDom = document.createElement('span');
            inputResizeDom.setAttribute('class', 'hide-dom');
            document.getElementsByTagName('body')[0].appendChild(inputResizeDom);
        }
    }

    function input_resize(html) {
        inputResizeDom.innerHTML = html;
        inputResizeDom.style.fontSize = '16px';
        return inputResizeDom.getBoundingClientRect().width.toFixed(4);
    }


    function insertHtmlAtCaret(html) {

        var sel, range;

        if (window.getSelection) {

            sel = window.getSelection();

            if (sel.getRangeAt && sel.rangeCount) {

                range = sel.getRangeAt(0);

                range.deleteContents();

                var el = document.createElement("input");

                el.innerHTML = 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") {
            document.selection.createRange().pasteHTML(html);
        }
    }
</script>
</body>
</html>

这是一个比较全的代码,当然还是有参考文章的
js之向div contenteditable光标位置添加字符
js-处理div设置的编辑框处理焦点定位追加内容

相关文章

  • 关于div标签可编辑且插入其它标签的问题

    落花有意随流水,流水无心蝶恋花 最近在做一个类似下面的功能: 这是一个比较全的代码,当然还是有参考文章的js之向d...

  • 节点的属性方法

    创建节点 document.createElement(新标签); 创建了一个新的div标签 插入节点(一) do...

  • HTML5结构标签

    div、section、articl语义逐渐增强 div div标签在页面中非常常见,也常将其称为标签容器。我们可...

  • 弹性布局图片变形

    问题图示 变形的图片 解决方案 方案1:用 div 标签包裹 img 标签用div标签包裹图片,这种方案比较通用。...

  • 网页设计:HTML常用的五种标签

    div标签 p标签 span标签 a标签 img标签 html:超级文本标记语言 1,div标签: 是在html中...

  • Ueditor 把div标签默认的变成p标签

    问题描述 当我们把html复制到ueditor中进行编辑的时候会发现原本文件中的div标签变成了p标签,这就导致有...

  • html标签

    第一章标签 1、标题标签: 2、段落标签: 3、水平线标签: 4、换行标签: 5、盒子:div、span(可同行)...

  • webstorm快捷键

    纯标签补全 div + tab 标签 + 文字 div{text} + tab 纯标签+id div#id + t...

  • HTML标签

    目录 HTML常用标签 关于标签问题及使用 HTML常用标签 创建一个HTML文档 设置文档标题和其它在网页中...

  • 居中对齐(vertical-align: middle/alig

    在一个小例子中遇到一个问题:一个div里有多个p标签,不管p标签内容多少,多个p标签必须高度(自定义)相同且文字居...

网友评论

      本文标题:关于div标签可编辑且插入其它标签的问题

      本文链接:https://www.haomeiwen.com/subject/eckenctx.html