就把遇到的問題記錄一下。寫這篇文章時用的TinyMCE編輯器就很強大,但畢竟是第三方的,項目也考慮了這些,如果做些自定義的東西不太方便。
1. 判斷光標位置的元素(或者選中的部分)的樣式。光標位置改變的時候更新工具欄對應按鈕的樣式。什么情況下光標的位置會改變呢?是鍵盤方向鍵和鼠標點擊,于是就判斷鍵盤事件和鼠標事件來執行光標移動的處理。
a. 獲得光標位置或選中元素:首先getSelection,創建range。然后獲得元素,獲取到元素之后就可以或得樣式、tagName等等,做更多的操作,運行代碼:
復制代碼 代碼如下:
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
html>
head>
title>/title>
meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
style type="text/css">
p{width:600px;text-align: left;text-indent:2em;line-height:20px;font-size:12px}
textarea{width:600px;height:100px;font-size:12px;overflow:auto}
/style>
/head>
body>
span style="display:block;height:150px; font-size:12px;line-height:150%">信息/span>
script type="text/javascript">
function createEditor(){
var iframe = document.createElement('iframe');
iframe.id = 'iframe';
iframe.frameBorder = 1;
iframe.width = 400;
iframe.height = 200;
document.body.appendChild(iframe);
return iframe;
}
var bind = function(element,eventType,fn,useCapture){
useCapture = useCapture || false;
if(arguments.length 3){
return true
};
if(window.addEventListener){
element.addEventListener(eventType, fn, useCapture);
}else{
element.attachEvent('on'+eventType,fn, useCapture);
}
}
//from 司徒正美
var css = document.defaultView ? function(el,style){
return document.defaultView.getComputedStyle(el, null).getPropertyValue(style)
} : function(el,style){
style = style.replace(/\-(\w)/g, function($, $1){
return $1.toUpperCase();
});
return el.currentStyle[style];
}
function bindEditor(){
var iframe = createEditor();
var ifr_win = iframe.contentWindow;
var ifr_doc = ifr_win.document;
var editorContent = 'span style="font-family: 黑體; font-weight: bold;">阿四大四大四/span>大span style="font-style: italic; text-decoration: underline;">四大四大打算/span>打打span style="font-style: italic; color: #ff0000;">雙打薩斯/span>大師';
ifr_doc.designMode='On';//可編輯
ifr_doc.contentEditable = true;
ifr_doc.open();
ifr_doc.writeln('html>head>style type="text/css">body{padding:10px;margin:0;font-size:13px;font-family:宋體;text-align:left;overflow:auto;word-wrap: break-word;cursor:text;background-color: transparent; }body,p,font,div,ul,li {line-height: 1.5;}p,font,div,ul,li {line-height: 1.5;margin:0;padding:0}a{color:#548DD4}/style>/head>body>'+ editorContent +'/body>/html>');
ifr_doc.close();
var getRange = function(){
var range = window.getSelection ? ifr_win.getSelection() : ifr_win.document.selection;
if (!range) {
return {
node : null,
range : null,
text : null
};
}
range = range.createRange ? range.createRange() : range.getRangeAt(0);
var text = window.getSelection ? range : range.text;
var rangeNode = null;
if (range.commonAncestorContainer) {
rangeNode = range.commonAncestorContainer;
} else {
if (range.parentElement) rangeNode = range.parentElement();
}
return {
node : rangeNode,
range : range,
text : text
}
}
var info = document.getElementsByTagName('span')[0];
var getStyle = function(node){
//console.log(node)
var html = '';
html+= 'span style="font-family:'+ css(node,'font-family') +'">字體:'+ css(node,'font-family') + '/span>br />';
html+= 'span style="color:'+ css(node,'color') +'">顏色:'+ css(node,'color') + '/span>br />';
html+= 'span style="font-style:'+ css(node,'font-style') +'">斜體:'+ css(node,'font-style') + '/span>br />';
html+= 'span style="font-weight:'+ css(node,'font-weight') +'">粗體:'+ css(node,'font-weight') + '/span>br />';
html+= 'span style="text-decoration:'+ css(node,'text-decoration') +'">下劃線:'+ css(node,'text-decoration') + '/span>br />';
html+= 'tagName:'+ node.tagName + ',style:'+ node.getAttribute('style') +'br />';
info.innerHTML = html;
}
//當光標位置改變時候執行
var onselectionchange = function(event){
var e = event || window.event;
if(!e.keyCode)e.keyCode = e.which;
//方向鍵移動光標,獲取光標位置的dom
if((e.keyCode >= 37 e.keyCode = 40 )|| e.type == "click"){
var node = getRange().node;//獲取光標位置元素
if(node !== null){
while(node.nodeType != 1){
node = node.parentNode;
}
getStyle(node);
}
}
}
bind(ifr_doc,'click',onselectionchange,false);
bind(ifr_doc,'keydown',onselectionchange,false);
}
window.onload = function(){
bindEditor();
}
/script>
/body>
/html>
2. ie不能保持光標位置,這個是在添加超鏈接時候出現的問題,當不使用瀏覽器內置的輸入框,光標移動其他的文本域里,ie會失去所選中的部分,無法對選中的部分加鏈接了,解決辦法就是:利用range的getBookmark和moveToBookmark,然后給iframe的document綁定onbeforedeactivate(getBookmark)、onactivate(moveTo),這2個事件的大致意思就是,當被激活和失去激活狀態。增加事件之后,就不必保存lastRang或者再其他地方設置bookmark了,可以讓ie像其他瀏覽器一樣自動保持光標位置了
復制代碼 代碼如下:
if(Util.browser.msie){
Util.bind(this.E.ifr_win.document, "beforedeactivate", function(){
var Rng = _self.getRange().range;
_self.rangeBookMark= Rng.getBookmark();
});
Util.bind(this.E.ifr_win.document, "activate", function(){
var Rng = _self.getRange().range;
Rng.moveToBookmark(_self.rangeBookMark);
Rng.select();
_self.rangeBookMark = null;
});
}
3. ie中的撤銷與重做 。 當iframe外部有彈出窗口、或者修改html撤銷、重做功能將失效。只能歸為ie的bug了。。。。也許ie沒分清iframe和頁面的document,把他們的撤銷、重做混道義了。
如下:
復制代碼 代碼如下:
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
html>
head>
title>/title>
meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
style type="text/css">
p{width:600px;text-align: left;text-indent:2em;line-height:20px;font-size:12px}
textarea{width:600px;height:100px;font-size:12px;overflow:auto}
/style>
/head>
body>
span style="display:block;height:150px; font-size:12px;line-height:150%">信息/span>
div id="J_tool">
input type="button" command="Undo" value="撤銷" unselectable="on" />
input type="button" command="Redo" value="重做" unselectable="on" />
input type="button" command="Bold" value="粗體" unselectable="on" />
input type="button" command="Italic" value="斜體" unselectable="on" />
/div>
br />
input type="button" onclick="changeLayout()" value="點擊下,ie將無法撤銷、重做" />
br />
script type="text/javascript">
function changeLayout(){
var popwin = document.getElementById('popwin');
if(!popwin){
popwin = document.createElement('div');
popwin.id = 'popwin';
popwin.style.cssText = 'display:none;width:300px;height:150px;background-color:#ccc;position:absolute;left:0;top:0px;text-align:center;line-height:150px;';
popwin.innerHTML = '改變了layoud渲染,ie將無法撤銷、重做';
document.body.appendChild(popwin);
popwin.onclick= function(){this.style.display = 'none'};
}
popwin.style.display = popwin.style.display == 'none' ? 'block' : 'none';
}
function createEditor(){
var iframe = document.createElement('iframe');
iframe.id = 'iframe';
iframe.frameBorder = 1;
iframe.width = 400;
iframe.height = 200;
document.body.appendChild(iframe);
return iframe;
}
var bind = function(element,eventType,fn,useCapture){
useCapture = useCapture || false;
if(arguments.length 3){
return true
};
if(window.addEventListener){
element.addEventListener(eventType, fn, useCapture);
}else{
element.attachEvent('on'+eventType,fn, useCapture);
}
}
//from 司徒正美
var css = document.defaultView ? function(el,style){
return document.defaultView.getComputedStyle(el, null).getPropertyValue(style)
} : function(el,style){
style = style.replace(/\-(\w)/g, function($, $1){
return $1.toUpperCase();
});
return el.currentStyle[style];
}
function bindEditor(){
var iframe = createEditor();
var ifr_win = iframe.contentWindow;
var ifr_doc = ifr_win.document;
var editorContent = 'span style="font-family: 黑體; font-weight: bold;">阿四大四大四/span>大span style="font-style: italic; text-decoration: underline;">四大四大打算/span>打打span style="font-style: italic; color: #ff0000;">雙打薩斯/span>大師';
ifr_doc.designMode='On';//可編輯
ifr_doc.contentEditable = true;
ifr_doc.open();
ifr_doc.writeln('html>head>style type="text/css">body{padding:10px;margin:0;font-size:13px;font-family:宋體;text-align:left;overflow:auto;word-wrap: break-word;cursor:text;background-color: transparent; }body,p,font,div,ul,li {line-height: 1.5;}p,font,div,ul,li {line-height: 1.5;margin:0;padding:0}a{color:#548DD4}/style>/head>body>'+ editorContent +'/body>/html>');
ifr_doc.close();
var getRange = function(){
var range = window.getSelection ? ifr_win.getSelection() : ifr_win.document.selection;
if (!range) {
return {
node : null,
range : null,
text : null
};
}
range = range.createRange ? range.createRange() : range.getRangeAt(0);
var text = window.getSelection ? range : range.text;
var rangeNode = null;
if (range.commonAncestorContainer) {
rangeNode = range.commonAncestorContainer;
} else {
if (range.parentElement) rangeNode = range.parentElement();
}
return {
node : rangeNode,
range : range,
text : text
}
}
var info = document.getElementsByTagName('span')[0];
var getStyle = function(node){
//console.log(node)
var html = '';
html+= 'span style="font-family:'+ css(node,'font-family') +'">字體:'+ css(node,'font-family') + '/span>br />';
html+= 'span style="color:'+ css(node,'color') +'">顏色:'+ css(node,'color') + '/span>br />';
html+= 'span style="font-style:'+ css(node,'font-style') +'">斜體:'+ css(node,'font-style') + '/span>br />';
html+= 'span style="font-weight:'+ css(node,'font-weight') +'">粗體:'+ css(node,'font-weight') + '/span>br />';
html+= 'span style="text-decoration:'+ css(node,'text-decoration') +'">下劃線:'+ css(node,'text-decoration') + '/span>br />';
html+= 'tagName:'+ node.tagName + ',style:'+ node.getAttribute('style') +'br />';
info.innerHTML = html;
}
//當光標位置改變時候執行
var onselectionchange = function(event){
var e = event || window.event;
if(!e.keyCode)e.keyCode = e.which;
//方向鍵移動光標,獲取光標位置的dom
if((e.keyCode >= 37 e.keyCode = 40 )|| e.type == "click"){
var node = getRange().node;//獲取光標位置元素
if(node !== null){
while(node.nodeType != 1){
node = node.parentNode;
}
getStyle(node);
}
}
}
bind(ifr_doc,'click',onselectionchange,false);
bind(ifr_doc,'keydown',onselectionchange,false);
bind(document.getElementById('J_tool'),'click',function(event){
event = event || window.event;
var target = event.srcElement || event.target;
var command = target.getAttribute('command');
var param = target.getAttribute('param') || '';
ifr_doc.execCommand(command,false,param);
return false;
})
}
window.onload = function(){
bindEditor();
}
/script>
/body>
/html>
如何解決呢? 只能依靠javascript模擬撤銷與重做了。網絡這方面的資源還是不少的,就不在此詳細說明了
您可能感興趣的文章:- nodejs后臺集成ueditor富文本編輯器的實例
- Vue.js結合Ueditor富文本編輯器的實例代碼
- Javascript實現簡單的富文本編輯器附演示
- 學習js在線html(富文本,所見即所得)編輯器
- 19款Javascript富文本網頁編輯器
- 不到200行 JavaScript 代碼實現富文本編輯器的方法