zoukankan      html  css  js  c++  java
  • 【网页在线编辑】图文发送的模式

    1、需求

    网页在线编辑第三方插件很多,我需要做一个手机上发布图片+文字的精简版的编辑器,文字和图片就自上而下排列就完了。

    iframe的实现架构很多。

    2、实现

    2.1 iframe定义

    image

    2.2 编辑模式设置和焦点获取

    ifEdit = this.getElementByXid("ifEdit").contentWindow;
           
            //编辑模式
            ifEdit.document.designMode = "on";
            ifEdit.document.contentEditable = true;
           
            var GetPos = function(){
                //debugger;
                ifEdit.pos = ifEdit.document.selection.createRange();
            };
            ifEdit.document.body.onclick = GetPos;
            ifEdit.document.body.onselect = GetPos;
            ifEdit.document.body.onkeyup = GetPos;

    2.3 图片插入 函数

    function(htmlElement){
            "<p><br>" +  htmlElement + "</p>";
            ifEdit.focus();
            var _image = document.createElement("img");   
            _image.src=htmlElement;
            _image.border="0";
            //var ifTemp = document.getElementById("ifEdittemp");
            debugger;

             if (ifEdit.document.selection.type.toLowerCase() != "none")
             {
                 ifEdit.document.selection.clear() ;
             }
            ifEdit.pos.pasteHTML(_image.outerHTML);
           
            ifEdit.focus();
        };

    3、问题

    WeX5架构下,iframe没有selection这个属性。 dubugger发现event.target可以获取到当前Node

    3.1 初始化iframe

    ifEdit = this.getElementByXid("ifEdit").contentWindow;
           
            //编辑模式
            ifEdit.document.designMode = "on";
            ifEdit.document.contentEditable = true;
           
            var GetPos = function(event){
                debugger;
                ifEdit.pos = event.target;//ifEdit.document.selection.createRange();
            };
            ifEdit.document.body.onclick = GetPos;
            ifEdit.document.body.onselect = GetPos;
            ifEdit.document.body.onkeyup = GetPos;

    3.2 在body上添加子节点

    Model.prototype.insertHtml = function(htmlElement){
            "<p><br>" +  htmlElement + "</p>";
            ifEdit.focus();
            var _image = document.createElement("img");   
            _image.src=htmlElement;
            _image.border="0";
            //var ifTemp = document.getElementById("ifEdittemp");
            //debugger;
           
            /*
             if (ifEdit.document.selection.type.toLowerCase() != "none")
             {
                 ifEdit.document.selection.clear() ;
             }*/
            
                

            //pos未获取焦点,没有最后一个节点,最后一个节点等于当前节点,追加到最后
            if(!ifEdit.pos || !ifEdit.document.body.lastChild ||
                    ifEdit.document.body.lastChild == ifEdit.pos){
                ifEdit.document.body.appendChild(_image);
            }

            else{
                ifEdit.document.body.insertBefore(_image,ifEdit.pos.nextSibling);
            }
           
            ifEdit.focus();
        };

    好记性不如烂笔头
  • 相关阅读:
    C语言数据结构链表
    Servlet中对上传的图片进行大小变换
    网页中有几个框架,在其中一个框架中点击超链接刷新整个页面
    来园子开博了
    学习《java编程思想》导入作者的net.mindview包
    git常用命令汇总
    安装lessloader后,编译项目报错TypeError: this.getOptions is not a function
    数组学习二
    常见文件管理命令
    (转载)Shell语法
  • 原文地址:https://www.cnblogs.com/inns/p/5554471.html
Copyright © 2011-2022 走看看