为文本编辑器做的一个颜色选择器,在firefox中一切正常。在ie中却出现了很怪的问题,选中文本后点击“改变文字颜色”按钮,选择颜色后,选中文字颜色不变,光标跳到了文档的开头……而且每次点“改变文字颜色”,光标都跳到最前面。头疼…… 各位有空的就当解解闷,看看代码吧,帮我看看哪里出了问题。不胜感激!!!!!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<style type="text/css">
#td_code { padding:8px 3px; }
#td_editor { width:595px; border: 1px solid #c5c5c5; line-height:130%; }
#toolbar { margin:0; padding: 0; width: 100%; list-style: none; border-bottom: 1px solid #c5c5c5; background: url(../images/toolbarbg.gif) repeat-x; height: 31px; }
</style><body>
<script type="text/javascript">
// JavaScript Documentvar colorPicker = function(){ this.colorPool = ["#000000","#993300","#333300","#003300","#003366","#000080","#333399","#333333","#800000","#FF6600","#808000","#008000","#008080","#0000FF","#666699","#808080","#FF0000","#FF9900","#99CC00","#339966","#33CCCC","#3366FF","#800080","#999999","#FF00FF","#FFCC00","#FFFF00","#00FF00","#00FFFF","#00CCFF","#993366","#CCCCCC","#FF99CC","#FFCC99","#FFFF99","#CCFFCC","#CCFFFF","#99CCFF","#CC99FF","#FFFFFF"]}
colorPicker.prototype.initialize = function(idStr){
var count=0;
var html = '';
var self = this;
html+= '<table cellspacing="5" cellpadding="0" border="2" bordercolor="#000000" style="cursor:pointer;background:#ECE9D8" >';
// html+= '<tr><td align="center" colspan="8" width="160" height="20" id="currentColor" bgcolor="#ffffff">当前颜色</td></tr>';
for(var i=0;i<5;i++)
{
html+= "<tr>";
for(j=0;j<8;j++)
{
html+= '<td align="center" width="20" height="20" style="background:'+ this.colorPool[count]+'" onClick="('+ self.setColor +')(\''+ this.colorPool[count]+'\')"> </td>';
count++;
}
html+= "</tr>";
}
html+= '</table>';
this.trigger = document.getElementById(idStr);
this.div = document.createElement('div');
this.div.innerHTML = html;
this.div.id = 'myColorPicker';
this.div.style.position = 'absolute';
this.div.style.left = 0;
this.trigger.parentNode.appendChild(this.div);
this.hide();
this.trigger.onclick = function(){
if(self.div.style.display == 'none'){
self.show();
return false;
}else{
self.hide();
return false;
}
}
}
colorPicker.prototype.setColor = function(c){
proEditor.setColor(c);
}
colorPicker.prototype.hide = function(){
this.div.style.display = 'none'
}
colorPicker.prototype.show = function(){
this.div.style.display = 'block'
}function $(str){return document.getElementById(str)}
function a_addEvent(oElm , strEvent , fuc) {
strEvent = strEvent.replace(/^on/i , '') ;
if(window.attachEvent)
oElm.attachEvent('on' + strEvent , fuc) ;
else
oElm.addEventListener(strEvent , fuc , false) ;
}
function stopEvent(evt , type){
if (evt.preventDefault) {
if(type == 'v' || type == null) evt.preventDefault();
if(type == 'b' || type == null) evt.stopPropagation();
}
else{
if(type == 'v' || type == null) evt.returnValue = false;
if(type == 'b' || type == null) evt.cancelBubble = true;
}
}
// ======================= 编辑器 =======================
var proEditor = { ed : null ,
edt : null,
storeBak : '', set : function(s , v) { this.ed.focus() ;
this.ed.document.execCommand(s,false,v||null);
} , display : function(ele , type , event) {
this.hide(ele) ;
switch(type) {
case 'color' :
break ;
}
} , hide : function(ele) {
} , onload : function(content) {
var test = new colorPicker() ;
test.initialize('dlg_setColor');
content = content || '' ;
this.ed = document.getElementById("ifm_editarea").contentWindow;
this.edt = document.getElementById("ifm_editarea").contentWindow.document; try{
this.ed.document.designMode = 'On';
this.ed.document.contentEditable = true;
}
catch(e) {
alert('目前只支持IE5.5,Firefox1.0+')
} this.ed.document.open();
this.ed.document.writeln('<html><head><style type="text/css">body { margin: 0; padding: 0px;}</style></head><body>' + content + '</body></html>')
this.ed.document.close();
var thisObj = this ;
a_addEvent(document , "onclick" , function() {thisObj.hide()}) ;
a_addEvent(this.ed.document , "onclick" , function() {thisObj.hide()}) ;
} , setColor : function(color) {
this.set('forecolor' , color) ;
document.getElementById('myColorPicker').style.display = 'none'
} , editorHTML : '<div id="td_editor" style="margin:0 auto;width:645px">' +
'<ul id="toolbar" onclick="stopEvent(event , \'b\')" >' +
'<li style="position: relative;"><a href="javascript://" id="dlg_setColor" onclick="proEditor.display(this,\'color\' , event)" title="颜色"><img border="0" width="169" height="35" alt="点击更换文字颜色" title="点击更换文字颜色" src="" /></a></li>' +
'</ul>' +
'<iframe id="ifm_editarea" src="about:blank" frameborder="0"></iframe>' +
'</div>' +
</div>'+
'<div class="rBot"></div></div>',
create : function(){
var div = document.createElement('div');
div.innerHTML = this.editorHTML;
document.body.appendChild(div)
div.style.position = 'absolute';
div.style.left = 0;
div.style.top = 0;
proEditor.onload();
}
}
</script><script type="text/javascript">
proEditor.create();
</script></body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<style type="text/css">
#td_code { padding:8px 3px; }
#td_editor { width:595px; border: 1px solid #c5c5c5; line-height:130%; }
#toolbar { margin:0; padding: 0; width: 100%; list-style: none; border-bottom: 1px solid #c5c5c5; background: url(../images/toolbarbg.gif) repeat-x; height: 31px; }
</style><body>
<script type="text/javascript">
// JavaScript Documentvar colorPicker = function(){ this.colorPool = ["#000000","#993300","#333300","#003300","#003366","#000080","#333399","#333333","#800000","#FF6600","#808000","#008000","#008080","#0000FF","#666699","#808080","#FF0000","#FF9900","#99CC00","#339966","#33CCCC","#3366FF","#800080","#999999","#FF00FF","#FFCC00","#FFFF00","#00FF00","#00FFFF","#00CCFF","#993366","#CCCCCC","#FF99CC","#FFCC99","#FFFF99","#CCFFCC","#CCFFFF","#99CCFF","#CC99FF","#FFFFFF"]}
colorPicker.prototype.initialize = function(idStr){
var count=0;
var html = '';
var self = this;
html+= '<table cellspacing="5" cellpadding="0" border="2" bordercolor="#000000" style="cursor:pointer;background:#ECE9D8" >';
// html+= '<tr><td align="center" colspan="8" width="160" height="20" id="currentColor" bgcolor="#ffffff">当前颜色</td></tr>';
for(var i=0;i<5;i++)
{
html+= "<tr>";
for(j=0;j<8;j++)
{
html+= '<td align="center" width="20" height="20" style="background:'+ this.colorPool[count]+'" onClick="('+ self.setColor +')(\''+ this.colorPool[count]+'\')"> </td>';
count++;
}
html+= "</tr>";
}
html+= '</table>';
this.trigger = document.getElementById(idStr);
this.div = document.createElement('div');
this.div.innerHTML = html;
this.div.id = 'myColorPicker';
this.div.style.position = 'absolute';
this.div.style.left = 0;
this.trigger.parentNode.appendChild(this.div);
this.hide();
this.trigger.onclick = function(){
if(self.div.style.display == 'none'){
self.show();
return false;
}else{
self.hide();
return false;
}
}
}
colorPicker.prototype.setColor = function(c){
proEditor.setColor(c);
}
colorPicker.prototype.hide = function(){
this.div.style.display = 'none'
}
colorPicker.prototype.show = function(){
this.div.style.display = 'block'
}function $(str){return document.getElementById(str)}
function a_addEvent(oElm , strEvent , fuc) {
strEvent = strEvent.replace(/^on/i , '') ;
if(window.attachEvent)
oElm.attachEvent('on' + strEvent , fuc) ;
else
oElm.addEventListener(strEvent , fuc , false) ;
}
function stopEvent(evt , type){
if (evt.preventDefault) {
if(type == 'v' || type == null) evt.preventDefault();
if(type == 'b' || type == null) evt.stopPropagation();
}
else{
if(type == 'v' || type == null) evt.returnValue = false;
if(type == 'b' || type == null) evt.cancelBubble = true;
}
}
// ======================= 编辑器 =======================
var proEditor = { ed : null ,
edt : null,
storeBak : '', set : function(s , v) { this.ed.focus() ;
this.ed.document.execCommand(s,false,v||null);
} , display : function(ele , type , event) {
this.hide(ele) ;
switch(type) {
case 'color' :
break ;
}
} , hide : function(ele) {
} , onload : function(content) {
var test = new colorPicker() ;
test.initialize('dlg_setColor');
content = content || '' ;
this.ed = document.getElementById("ifm_editarea").contentWindow;
this.edt = document.getElementById("ifm_editarea").contentWindow.document; try{
this.ed.document.designMode = 'On';
this.ed.document.contentEditable = true;
}
catch(e) {
alert('目前只支持IE5.5,Firefox1.0+')
} this.ed.document.open();
this.ed.document.writeln('<html><head><style type="text/css">body { margin: 0; padding: 0px;}</style></head><body>' + content + '</body></html>')
this.ed.document.close();
var thisObj = this ;
a_addEvent(document , "onclick" , function() {thisObj.hide()}) ;
a_addEvent(this.ed.document , "onclick" , function() {thisObj.hide()}) ;
} , setColor : function(color) {
this.set('forecolor' , color) ;
document.getElementById('myColorPicker').style.display = 'none'
} , editorHTML : '<div id="td_editor" style="margin:0 auto;width:645px">' +
'<ul id="toolbar" onclick="stopEvent(event , \'b\')" >' +
'<li style="position: relative;"><a href="javascript://" id="dlg_setColor" onclick="proEditor.display(this,\'color\' , event)" title="颜色"><img border="0" width="169" height="35" alt="点击更换文字颜色" title="点击更换文字颜色" src="" /></a></li>' +
'</ul>' +
'<iframe id="ifm_editarea" src="about:blank" frameborder="0"></iframe>' +
'</div>' +
</div>'+
'<div class="rBot"></div></div>',
create : function(){
var div = document.createElement('div');
div.innerHTML = this.editorHTML;
document.body.appendChild(div)
div.style.position = 'absolute';
div.style.left = 0;
div.style.top = 0;
proEditor.onload();
}
}
</script><script type="text/javascript">
proEditor.create();
</script></body>
</html>
解决方案 »
- 今天做页面的时候遇到一个tabpanel的问题,不知道是不是extjs的bug
- js的Array问题
- 使用openlayers进行GIS地图制作中,经过那些处理可以是网站速度加快啊??
- 难倒了,为什么左漂图片放在网上去就不漂浮了
- 初学者问题:页面刷新后,如何保存textbox的值不被置空?
- 这段代码怎么在IE中不能运行,但是firefox中可以
- 如何用 js 在页面中做一个 swing/swt 中那样的树结构?
- 请问: javascript 语句 在html文件中能判断 文件存在吗? 是不是不行呀??? [asp 又一个 Filesystemscrpt 啥动动?] 我急需用 java
- 我遇到一个难题:如何点击checkbox的时候相当于按下"enter"键!!!!!!!!!!!!!!!!!!!
- 有关setAttribute的问题?
- jsp不刷新页面传值问题
- 想问下Qzone(QQ空间)中屏幕截图是怎么实现的呢
proEditor 未定义....
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<style type="text/css">
#td_code { padding:8px 3px; }
#td_editor { width:595px; border: 1px solid #c5c5c5; line-height:130%; }
#toolbar { margin:0; padding: 0; width: 100%; list-style: none; border-bottom: 1px solid #c5c5c5; background: url(../images/toolbarbg.gif) repeat-x; height: 31px; }
</style><body>
<script type="text/javascript">
// JavaScript Documentvar colorPicker = function(){ this.colorPool = ["#000000","#993300","#333300","#003300","#003366","#000080","#333399","#333333","#800000","#FF6600","#808000","#008000","#008080","#0000FF","#666699","#808080","#FF0000","#FF9900","#99CC00","#339966","#33CCCC","#3366FF","#800080","#999999","#FF00FF","#FFCC00","#FFFF00","#00FF00","#00FFFF","#00CCFF","#993366","#CCCCCC","#FF99CC","#FFCC99","#FFFF99","#CCFFCC","#CCFFFF","#99CCFF","#CC99FF","#FFFFFF"]}
colorPicker.prototype.initialize = function(idStr){
var count=0;
var html = '';
var self = this;
html+= '<table cellspacing="5" cellpadding="0" border="2" bordercolor="#000000" style="cursor:pointer;background:#ECE9D8" >';
// html+= '<tr><td align="center" colspan="8" width="160" height="20" id="currentColor" bgcolor="#ffffff">当前颜色</td></tr>';
for(var i=0;i<5;i++)
{
html+= "<tr>";
for(j=0;j<8;j++)
{
html+= '<td align="center" width="20" height="20" style="background:'+ this.colorPool[count]+'" onClick="('+ self.setColor +')(\''+ this.colorPool[count]+'\')"> </td>';
count++;
}
html+= "</tr>";
}
html+= '</table>';
this.trigger = document.getElementById(idStr);
this.div = document.createElement('div');
this.div.innerHTML = html;
this.div.id = 'myColorPicker';
this.div.style.position = 'absolute';
this.div.style.left = 0;
this.trigger.parentNode.appendChild(this.div);
this.hide();
this.trigger.onclick = function(){
if(self.div.style.display == 'none'){
self.show();
return false;
}else{
self.hide();
return false;
}
}
}
colorPicker.prototype.setColor = function(c){
proEditor.setColor(c);
}
colorPicker.prototype.hide = function(){
this.div.style.display = 'none'
}
colorPicker.prototype.show = function(){
this.div.style.display = 'block'
}function $(str){return document.getElementById(str)}
function a_addEvent(oElm , strEvent , fuc) {
strEvent = strEvent.replace(/^on/i , '') ;
if(window.attachEvent)
oElm.attachEvent('on' + strEvent , fuc) ;
else
oElm.addEventListener(strEvent , fuc , false) ;
}
function stopEvent(evt , type){
if (evt.preventDefault) {
if(type == 'v' || type == null) evt.preventDefault();
if(type == 'b' || type == null) evt.stopPropagation();
}
else{
if(type == 'v' || type == null) evt.returnValue = false;
if(type == 'b' || type == null) evt.cancelBubble = true;
}
}
// ======================= 编辑器 =======================
var proEditor = { ed : null ,
edt : null,
storeBak : '', set : function(s , v) { this.ed.focus() ;
this.ed.document.execCommand(s,false,v||null);
} , display : function(ele , type , event) {
this.hide(ele) ;
switch(type) {
case 'color' :
break ;
}
} , hide : function(ele) {
} , onload : function(content) {
var test = new colorPicker() ;
test.initialize('dlg_setColor');
content = content || '' ;
this.ed = document.getElementById("ifm_editarea").contentWindow;
this.edt = document.getElementById("ifm_editarea").contentWindow.document; try{
this.ed.document.designMode = 'On';
this.ed.document.contentEditable = true;
}
catch(e) {
alert('目前只支持IE5.5,Firefox1.0+')
} this.ed.document.open();
this.ed.document.writeln('<html><head><style type="text/css">body { margin: 0; padding: 0px;}</style></head><body>' + content + '</body></html>')
this.ed.document.close();
var thisObj = this ;
a_addEvent(document , "onclick" , function() {thisObj.hide()}) ;
a_addEvent(this.ed.document , "onclick" , function() {thisObj.hide()}) ;
} , setColor : function(color) {
this.set('forecolor' , color) ;
document.getElementById('myColorPicker').style.display = 'none'
} , editorHTML : '<div id="td_editor" style="margin:0 auto;width:645px">' +
'<ul id="toolbar" onclick="stopEvent(event , \'b\')" >' +
'<li style="position: relative;"><a href="javascript://" id="dlg_setColor" onclick="proEditor.display(this,\'color\' , event)" title="颜色"><img border="0" width="169" height="35" alt="点击更换文字颜色" title="点击更换文字颜色" src="" /></a></li>' +
'</ul>' +
'<iframe id="ifm_editarea" src="about:blank" frameborder="0"></iframe>' +
'</div>' +
'</div>'+
'<div class="rBot"></div></div>',
create : function(){
var div = document.createElement('div');
div.innerHTML = this.editorHTML;
document.body.appendChild(div)
div.style.position = 'absolute';
div.style.left = 0;
div.style.top = 0;
proEditor.onload();
}
}
</script><script type="text/javascript">
proEditor.create();
</script></body>
</html>
a.frame.focus()IE 的设置颜色要这样写
if(a.iframe.rng)
a.iframe.rng.select();//$.browser.msie 判定是否IE
if(!$.browser.msie)
a.doc.execCommand('useCSS',false,false);a.frame.focus();
a.doc.execCommand(this.colorcmd,false,b);if(!$.browser.msie)
a.doc.execCommand('useCSS',false,true);
a.frame.focus()
其实不然.
当浏览者在点击你的图片按按扭的时候,焦点就已经不在浏览器上了.
而恰恰IE没有给提供记住焦点的功能.
LZ要是想做一个大型的editor建议将焦点缓存起来(譬如放在你的proEditor 中)以便以后应付更复杂的焦点问题.
但是,LZ这个可以给你的按扭和弹出层用"不获取焦点"的属性 unselectable="on"来解决.
如:<a href="javascript://" id="dlg_setColor" onclick="proEditor.display(this,\'color\' , event)" title="颜色"><img border="0" width="169" height="35" alt="点击更换文字颜色" title="点击更换文字颜色" src="" /></a>
其实可以不用a标签,直接
<img border="0" width="169" height="35" id="dlg_setColor" onclick="proEditor.display(this,\'color\' , event)" alt="点击更换文字颜色" title="点击更换文字颜色" src="" style="cursor:pointer;" unselectable="on"/>还有弹出层上:
html+= '<td align="center" width="20" height="20" style="background:'+ this.colorPool[count]+'" onClick="('+ self.setColor +')(\''+ this.colorPool[count]+'\')" unselectable="on"> </td>';
this.ed.document.execCommand(s,false,v||null);
this.ed.focus();
谢谢
选中范围为 sel.getRangeAt(0);
在选中范围内可以有相应的一些方法.比如insertNode ,cloneRange 等等.
可以将选中范围或选区放在外部变量或特定存储区域中,以便后来再用.
具体方法参见mozilla的文档.
在IE中就比较熟悉了.
但是选区不在window域下,而在document下
var sel=document.selection;
选中范围为 range = sel.createRange();
在选中范围内可以有相应的一些方法.比如pasteHTML,select等等(不和mozilla一样的).总之要做好兼容很麻烦.建议为他做个适配器,将方法统一出来