0

0

自己写的兼容ie和ff的在线文本编辑器类似ewebeditor_其他特效

php中文网

php中文网

发布时间:2016-05-16 17:46:47

|

1339人浏览过

|

来源于php中文网

原创

怎么说呢,刚包完夜吧,应该很累了,但现在仍有力气敲打着这些字符,看来我还没有到此为止啊。
废话少说,最近写了个在线的编辑器,类似ewebeditor那样的,当然没有人家那么强大,但是基本功能都有,而且还是兼容ie和ff的,为此我也花了不少功夫,还是赶紧把代码祭出来吧
demo.html

FaceSwapper
FaceSwapper

FaceSwapper是一款AI在线换脸工具,可以让用户在照片和视频中无缝交换面孔。

下载
复制代码 代码如下:
















字体:
字号:



















core.js
复制代码 代码如下:

sx={};
sx.comm={};
sx.comm.string=function(){
if(!String.prototype.left){
String.prototype.left=function(l){
return this.substr(0,l);
}
}
if(!String.prototype.right){
String.prototype.right=function(l){
return this.substr(this.length-l,l);
}
}
if(!String.prototype.trim){
String.prototype.trim=function(){
return this.replace(/^/s+|/s+$/g,"");
}
}
}();
sx.comm.array=function(){
if(!Array.prototype.indexOf){
Array.prototype.indexOf=function(data){
for(var i=0;iif(this[i]==data){
break;
}
}
return i==this.length?-1:i;
}
}
if(!Array.prototype.lastIndexOf){
Array.prototype.lastIndexOf=function(data){
for(var i=this.length-1;i>=0;i--){
if(this[i]==data){
break;
}
}
return i;
}
}
if(!Array.prototype.clone){
Array.prototype.clone=function(){
var temp=[];
for(var i=0;iif(this[i] instanceof Array){
temp[i]=this[i].clone();
}else{
temp[i]=this[i];
}
}
return temp;
}
}
if(!Array.prototype.every){
Array.prototype.every=function(o,f){
for (var i = 0; i if (this[i] instanceof Array) {
this[i].every(o,f);
}
else {
f.call(o, this[i]);
}
}
}
}
}();
sx.comm.ver=function(){
try{
HTMLElement;
return "ff";
}catch(e){
return "ie";
}
}();
sx.comm.ext=function(){
if(sx.comm.ver=="ff"){
HTMLElement.prototype.__defineGetter__("outerHTML",function(){
var attr;
var attrs=this.attributes;
var str="for(var i=0;iattr=attrs[i];
if(attr.specified)
str+=" "+attr.name+'="'+attr.value+'"';
}
if(!this.canHaveChildren)
return str+">";
return str+">"+this.innerHTML+""+this.tagName.toLowerCase()+">";
});
HTMLElement.prototype.__defineGetter__("canHaveChildren",function(){
switch(this.tagName.toLowerCase()){
case "area":
case "base":
case "basefont":
case "col":
case "frame":
case "hr":
case "img":
case "br":
case "input":
case "isindex":
case "link":
case "meta":
case "param":
return false;
}
return true;
});
XMLDocument.prototype.selectNodes = Element.prototype.selectNodes = function (){
//alert(arguments[0]);
var oNSResolver = this.createNSResolver(this.documentElement)
var aItems = this.evaluate(arguments[0].toLowerCase(), this, oNSResolver,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
var aResult = [];
for( var i = 0; i {
aResult[i] = aItems.snapshotItem(i);
}
//alert(aItems.snapshotLength);
return aResult;
}
}
}();
sx.event={};
sx.event.target=function(){
if(window.event){
return window.event.srcElement;
}else{
var f=arguments.callee.caller;
while(f){
if(f.arguments[0] instanceof Event){
return f.arguments[0].target;
}
f=f.caller;
}
}
}
sx.event.event=function(){
if (window.event) {
return window.event;
}else{
var f=arguments.callee.caller;
while (f) {
if (f.arguments[0] instanceof Event) {
return f.arguments[0];
}
f = f.caller;
}
}
}
sx.event.relatedtarget=function(){
if(window.event){
if(window.event.type=="mouseover"){
return window.event.fromElement;
}else if(window.event.type=="mouseout"){
return window.event.toElement;
}
}else{
var f=arguments.callee.caller;
while (f) {
if (f.arguments[0] instanceof Event) {
return f.arguments[0].relatedTarget;
}
f = f.caller;
}
}
}
sx.event.stopevent=function(){
if (window.event) {
window.event.returnValue=false;
window.event.cancelBubble=true;
}else{
var f=arguments.callee.caller;
while(f){
if(f.arguments[0] instanceof Event){
break;
}
f=f.caller;
}
f.arguments[0].preventDefault();
f.arguments[0].stopPropagation();
}
}
sx.event.addevent=function(e,t,f){
if(!arguments.callee.event){
arguments.callee.event=[];
}
if(e.attachEvent){
e.attachEvent("on"+t,f);
}else{
e.addEventListener(t,f,false);
}
arguments.callee.event.push(f);
return arguments.callee.event.length-1;
}
sx.event.removeevent=function(e,t,i){
if(e.detachEvent){
e.detachEvent("on"+t,sx.event.addevent.event[i]);
}else{
e.removeEventListener(t,sx.event.addevent.event[i],false);
}
sx.event.addevent.event[i]=null;
}
sx.event.parseevent=function(e,t){
if (sx.comm.ver=="ie"){
e.fireEvent("on"+t);
}else{
var evt = document.createEvent("Events");
evt.initEvent(t, true, true);
e.dispatchEvent(evt);
}
}
sx.dom={};
sx.dom.text=function(e){
return this.e.innerText?this.e.innerText:this.e.innerHTML.replace(//<.>/igm,"");
}
sx.dom.elementnodes=function(e,flag){
var temp=[];
var a=e.childNodes;
for(var i=0;iif(a[i].nodeType==flag){
temp.push(a[i]);
}
}
return temp;
}
sx.dom.elementallnodes=function(e,flag){
var temp=[];
var a=e.getElementsByTagName("*");
for(var i=0;iif(a[i].nodeType==flag){
temp.push(a[i]);
}
}
return temp;
}
sx.dom.xpath=function(e,mode){
p=e.cloneNode(true);
var s=p.getElementsByTagName("script");
for(var i=0;ip.replaceChild(s[i].cloneNode(false),s[i]);
var html=p.outerHTML.replace(//=(?!"|')(.*?)(?=/s|>)/ig,"=/"$1/"");
if(window.ActiveXObject){
var x=new ActiveXObject("Msxml2.DOMDocument");
x.async=false;
x.loadXML(""+html);
}else{
var oParser = new DOMParser();
//alert(html);
var x = oParser.parseFromString(html,"text/xml");
//alert(x.documentElement.tagName);
}
var div=x.selectNodes(mode);
//alert(div.length);
var temp=[];
var a1=x.selectNodes(e.tagName.toUpperCase()+"//*");
//alert(a1.length);
var all=e.getElementsByTagName("*");
//alert(all.length);
var i1=0;
for(i=0;i//alert(i);
if(a1[i]==div[i1]){
temp.push(all[i]);
i1++;
}
}
x=null;
return temp;
}
sx.dom.left=function(e){
if(document.getBoundingClientRect){
return e.getBoundingClientRect().left;
}else{
var a=e;
var left=0;
while(a){
left+=a.offsetLeft;
a=a.offsetParent;
}
return left;
}
}
sx.dom.top = function(e){
if(document.getBoundingClientRect){
return e.getBoundingClientRect().top;
}else{
var a=e;
var top=0;
while(a){
top+=a.offsetTop;
a=a.offsetParent;
}
return top;
}
}
sx.dom.getstyle=function(e,prop){
if(e.currentStyle){
return e.currentStyle[prop];
}else{
return document.defaultView.getComputedStyle(e,null).getPropertyValue(prop);
}
}
sx.dom.setstyle=function(e,data){
for(var i in data){
e.style[i]=data[i];
}
}
advance.js:
var $=function(id){
return document.getElementById(id);
}

edit.js
复制代码 代码如下:

var w=$("content").contentWindow;
w.document.designMode="on";
w.document.open();
w.document.write("")
w.document.close();
if(sx.comm.ver=="ie"){
//w.document.body.style.lineHeight="10px";
}
w.document.onkeydown=function(){
if(sx.comm.ver=="ie"){
if(w.event.keyCode==13){
var s=w.document.selection.createRange();
s.pasteHTML("
");
w.focus();
return false;
}
}
}
function wnd(){
var main=document.createElement("div");
sx.dom.setstyle(main,{
position:"absolute",
width:"100%",
height:"100%",
backgroundColor:"lightblue",
filter:"alpha(opacity=50)",
opacity:0.5
});
var body=document.createElement("div");
sx.dom.setstyle(body,{
position:"absolute",
width:"200px",
height:"250px",
backgroundColor:"green",
zIndex:1000
});
var title=document.createElement("div");
sx.dom.setstyle(title,{
width:"200px",
height:"20px",
backgroundColor:"blue",
});
var close=document.createElement("span");
sx.dom.setstyle(close,{
marginLeft:"180px",
display:"block",
width:"20px",
height:"20px",
textAlign:"center",
cursor:"pointer"
});
close.innerHTML="X";
close.onclick=function(){
main.parentNode.removeChild(main);
body.parentNode.removeChild(body);
}
title.appendChild(close);
body.appendChild(title);
var content=document.createElement("div");
sx.dom.setstyle(content,{
width:"200px",
height:"230px"
});
body.appendChild(content);
this.show=function(e){
document.body.appendChild(main);
sx.dom.setstyle(main,{
top:"0px",
left:"0px"
});
document.body.appendChild(body);
sx.dom.setstyle(body,{
top:sx.dom.top(e)+e.clientHeight+"px",
left:sx.dom.left(e)+e.clientWidth+"px",
});
}
this.close=close;
this.main=main;
this.body=body;
this.title=title;
this.content=content;
}
function bold(){
w.document.execCommand("bold",null,null);
}
function italic(){
w.document.execCommand("italic",null,null);
}
function left(){
w.document.execCommand("JustifyLeft",null,null);
}
function center(){
w.document.execCommand("Justifycenter",null,null);
}
function right(){
w.document.execCommand("Justifyright",null,null);
}
function FontName(value){
w.document.execCommand("FontName", false, value);
}
function FontSize(value){
w.document.execCommand("FontSize", false, value);
}
function inserthr(){
if(document.selection){
w.focus();
var s=w.document.selection.createRange();
s.pasteHTML("
");
}else{
w.focus();
var s=w.getSelection().getRangeAt(0);
s.insertNode(w.document.createElement("hr"));
}
}
function insertlink(){
if (document.selection) {
w.focus();
var s = w.document.selection.createRange();
}
else {
w.focus();
var s = w.getSelection().getRangeAt(0);
}
var e=sx.event.target();
var ww=new wnd();
ww.content.appendChild(document.createTextNode("请输入链接地址;"));
var link=document.createElement("input");
link.type="text";
link.size=20;
ww.content.appendChild(link);
var b=document.createElement("button");
b.innerHTML="确定";
ww.content.appendChild(b);
b.onclick=function(){
if(sx.comm.ver=="ie"){
s.pasteHTML(""+s.htmlText+"");
}
else{
var a=w.document.createElement("a");
a.href=link.value;
s.surroundContents(a);
}
sx.event.parseevent(ww.close,"click");
}
ww.show(e);
}
function inserttable(){
if (document.selection) {
w.focus();
var s = w.document.selection.createRange();
}
else {
w.focus();
var s = w.getSelection().getRangeAt(0);
}
var e=sx.event.target();
var ww=new wnd();
ww.content.appendChild(document.createTextNode("请输入行数;"));
var tr=document.createElement("input");
tr.type="text";
tr.size=20;
ww.content.appendChild(tr);
ww.content.appendChild(document.createElement("br"));
ww.content.appendChild(document.createTextNode("请输入列数;"));
var td=document.createElement("input");
td.type="text";
td.size=20;
ww.content.appendChild(td);
ww.content.appendChild(document.createElement("br"));
ww.content.appendChild(document.createTextNode("请输入单元格高度;"));
var height=document.createElement("input");
height.type="text";
height.size=20;
ww.content.appendChild(height);
ww.content.appendChild(document.createElement("br"));
ww.content.appendChild(document.createTextNode("请输入单元格宽度;"));
var width=document.createElement("input");
width.type="text";
width.size=20;
ww.content.appendChild(width);
ww.content.appendChild(document.createElement("br"));
var b=document.createElement("button");
b.innerHTML="确定";
ww.content.appendChild(b);
b.onclick=function(){
var l1=Number(tr.value);
var l2=Number(td.value);
var h1=Number(height.value);
var w1=Number(width.value);
ww.content.appendChild(document.createTextNode("请输入单元格高度;"));
var t=document.createElement("table");
t.border="1";
var tb=document.createElement("tbody");
t.appendChild(tb);
for(var i=0;ivar tr1=document.createElement("tr");
for(var i1=0;i1var td1=document.createElement("td");
td1.innerHTML="";
sx.dom.setstyle(td1,{
width:w1+"px",
height:h1+"px"
});
tr1.appendChild(td1);
}
tb.appendChild(tr1);
}
if(sx.comm.ver=="ie"){
s.pasteHTML(t.outerHTML);
}
else{
s.insertNode(t);
s.insertNode(document.createElement("br"));
}
sx.event.parseevent(ww.close,"click");
}
ww.show(e);
}
function color(){
var e=sx.event.target();
if (document.selection) {
w.focus();
var s = w.document.selection.createRange();
}
else {
w.focus();
var s = w.getSelection().getRangeAt(0);
}
var ww=new wnd();
var colors = ["00","33","66","99","CC","FF"];
var cp=document.createElement("span");
sx.dom.setstyle(cp,{
display:"inline-block",
width:"10px",
height:"10px",
margin:"2px"
});
for(var i1=5;i1>=0;i1--){
for(var i2=5;i2>=0;i2--){
for(var i3=5;i3>=0;i3--){
var cp1=cp.cloneNode(true);
cp1.style.backgroundColor="#" + colors[i1] + colors[i2] + colors[i3];
cp1.title="#" + colors[i1] + colors[i2] + colors[i3];
cp1.onclick=function(){
if(sx.comm.ver=="ie"){
w.focus();
s.pasteHTML(""+s.htmlText+"");
}
else{
var a=w.document.createElement("font");
a.color=this.title;
s.surroundContents(a);
}
sx.event.parseevent(ww.close,"click");
}
ww.content.appendChild(cp1);
}
}
}
ww.show(e);
}

关键是demo.html和edit.js里的代码,core.js和advance.js里的代码是我为兼容浏览器写的,本想把它扩展成一个完善的框架的,因为时间有限,就没写下去了。

本编辑器还没有实现图片和文件的上传,因为需要服务器技术,所以我就没写了,可以交给读者慢慢研究。
我打算先将web放放了,开始专心于vc++的探究上,尽量能写出一个像样的程序出来,以后如果有时间我也会继续完善这个编辑器以及javascript兼容的框架。
恩,好好加油吧。

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
go语言 注释编码
go语言 注释编码

本专题整合了go语言注释、注释规范等等内容,阅读专题下面的文章了解更多详细内容。

2

2026.01.31

go语言 math包
go语言 math包

本专题整合了go语言math包相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

go语言输入函数
go语言输入函数

本专题整合了go语言输入相关教程内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

golang 循环遍历
golang 循环遍历

本专题整合了golang循环遍历相关教程,阅读专题下面的文章了解更多详细内容。

0

2026.01.31

Golang人工智能合集
Golang人工智能合集

本专题整合了Golang人工智能相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

2026赚钱平台入口大全
2026赚钱平台入口大全

2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。

76

2026.01.31

高干文在线阅读网站大全
高干文在线阅读网站大全

汇集热门1v1高干文免费阅读资源,涵盖都市言情、京味大院、军旅高干等经典题材,情节紧凑、人物鲜明。阅读专题下面的文章了解更多详细内容。

73

2026.01.31

无需付费的漫画app大全
无需付费的漫画app大全

想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。

67

2026.01.31

漫画免费在线观看地址大全
漫画免费在线观看地址大全

想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。

19

2026.01.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
HTML教程
HTML教程

共500课时 | 5.2万人学习

【web前端】Node.js快速入门
【web前端】Node.js快速入门

共16课时 | 2万人学习

进程与SOCKET
进程与SOCKET

共6课时 | 0.4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号