台灣最大裝潢接案平台
輕鬆接案網
抓漏新屋裝潢.舊屋裝修.合格裝潢
裝修證照.是專業施工品質的保障

首頁  •  j2h 論壇 • 程式設計討論     • 

[JaveScript] IE 抓不到焦距 (簡易的所見即所得發文器

房東:LAIRLJ
發表時間:2012-12-25
[檢舉]


首先,我對於 javascript 語言完全一竅不通,頂多只是大約看得懂邏輯性而已,以下這些程式碼是我拿某些程式語言給拼拼湊湊、修修剪剪而來。
我的問題是使用 Chrome 和 Firefox 都大致上正常,惟獨對 IE 就失準。

在以下程式碼的結果,發文框內若按下預設的按鈕,Chrome 和 Firefox 都大概可以隨心所欲的插入某些本文之中,例如 在預設的按鈕 1 內容 和 在預設的按鈕 2 內容 之中 若想插入 預設的按鈕 3 內容 , Chrome 和 Firefox 只要把滑鼠指標給移到此兩段文字中間,再按下 按鈕 3 ,就很正常。然而 IE 就無法準確的插入其中,都會跑到最前段去,弄了好幾天,快投降了,請求諸位高手協助。

另外,若我想法這段程式碼內的 “加入顏色”的功能,原本是 拖曳文字內容 → 加入顏色鈕 → 按下色塊鈕 文字顯示變色,改成直接 拖曳文字內容 → 按下色塊鈕 文字顯示變色,而不必經由 “加入顏色”鈕的程序。
可以的話,請直接 Copy 底下程式碼,直接本機做測試。

以下為程式碼:
index.html


EDITOR TEXT










AAA

BBB

CCC

DDD

EEE


























以下為 editor.js

var toolbar ="DELcolor,SPACE,ADDcolor";
var theSelection = false;
var clientPC = navigator.userAgent.toLowerCase();
var clientVer = parseInt(navigator.appVersion);
var is_ie = ((clientPC.indexOf('msie') != -1) && (clientPC.indexOf('opera') == -1));
var is_win = ((clientPC.indexOf('win') != -10) || (clientPC.indexOf('16bit') != -10));

var array_color = new Array();
array_color[0] ="#000000";
array_color[1] ="#660000";
array_color[2] ="#990000";
array_color[3] ="#ff0000";
array_color[4] ="#006600";
array_color[5] ="#009900";
array_color[6] ="#00ff00";
array_color[7] ="#000066";
array_color[8] ="#000099";
array_color[9] ="#0000ff";
array_color[10] ="#666600";
array_color[11] ="#ff6600";
array_color[12] ="#66ff00";
array_color[13] ="#006666";
array_color[14] ="#0066ff";
array_color[15] ="#660066";
array_color[16] ="#ff0066";
array_color[17] ="#6600ff";
//---------------------------------------------------------------
var starup=0;
var rng;
var allRTEs="";
var isIE;
var isIE_Mac;
var isGecko;
var isOpera9;
var isSafari;
var isSafari3;
var isKonqueror;
var isICab;
var isMacOS;
var HTML_ON;
var chkVK=0;
var editor_size;
var currenteditor="";
var ua=navigator.userAgent.toLowerCase();
isIE=((ua.indexOf("msie")!=-1)&&(ua.indexOf("opera")==-1)&&(ua.indexOf("webtv")==-1));
isGecko=(ua.indexOf("gecko")!=-1&&ua.indexOf("safari")==-1);
isOpera9=(ua.indexOf("opera")!=-1&&ua.indexOf("safari")==-1);
isSafari=(ua.indexOf("gecko")!=-1&&ua.indexOf("safari")!=-1&&ua.indexOf("version/3")==-1);
isSafari3=(ua.indexOf("gecko")!=-1&&ua.indexOf("safari")!=-1&&ua.indexOf("version/3")!=-1);
isKonqueror=(ua.indexOf("konqueror")!=-1);isICab=(ua.indexOf("icab")!=-1);
isIE_Mac=(ua.indexOf("msie")!=-1&&ua.indexOf("mac")!=-1);
isMacOS=(ua.indexOf("macintosh")!=-1);
//--------------------------------------------------------------------------------------------------------------
function Instantiate(a,b,c) //("editor", 內容, "120px")
{
starup=1;
editor_size=parseInt(c);
if(allRTEs.length>0)allRTEs+=";";
allRTEs+=a;
b=BBCodeToHTML(b);
writeRTE(a,b,c,true,false);
}
//--------------------------------------------------------------------------------------------------------------
function enableDesignMode(a,b,c) // 內容顯示至輸入框 //("editor", 內容, 不成立)
{
b=b.replace(/&#/gi,"&#");
var d="\n";
d+="\n";
d+="\n";
d+="\n";
d+="\n";
d+="\n";
d+=b+"\n";
d+="\n";
d+="";
if(document.all)
{
var f=frames[a].document;
f.open();
f.write(d);
f.close();
if(!c){f.designMode="On";}
}

else
{
try{
if(!c)document.getElementById(a).contentDocument.designMode="on";
try{
var f=document.getElementById(a).contentWindow.document;
f.open();
if(isGecko||isSafari){f.write(d+"
");}
else{f.write(d);}
f.close();
}
catch(e){alert("Error preloading content.");}
}
catch(e){return false;}
}
}
//--------------------------------------------------------------------------------------------------------------
function setCodeOutput()
{
var a=allRTEs.split(";");
for(var i=0;i }
//--------------------------------------------------------------------------------------------------------------

function updateRTE(a)
{
starup=0;
//if(HTML_ON=="no"){document.getElementById("chkSrc"+a).checked=false;toggleHTMLSrc(a)}
var b=document.getElementById(a).contentWindow.document.body.innerHTML;
b=b.replace(/
<\/div>/ig,"");
b=b.replace(/]*>/ig,"
");
b=b.replace(/[\n\r]/ig,'');
b=HTMLToBBCode(b);
document.getElementById('bbcode_ouput_'+a).value=b;
document.getElementById('html_ouput_'+a).value=BBCodeToHTML(b);
}
//--------------------------------------------------------------------------------------------------------------
function writeRTE(a,b,c,d,e) //("editor", 內容, "120px", 成立, 不成立)
{
document.write("
\n");
array=toolbar.split(",");
for(i=0;i<=array.length;i++){if(array[i])show_toolbar(array[i],a);}
document.write("
\n");
document.writeln("
");
for (i=0;i {
document.writeln("
");
document.writeln("
");
}// 顏色塊 0
document.writeln("

");
document.write("\n");
document.write('');
document.write('');
if(!document.getElementById("hotmem"))document.write('');

enableDesignMode(a,b,e);
}
//--------------------------------------------------------------------------------------------------------------
function show_toolbar(a,b) //顯示工具按鈕
{
a=a.replace(" ","");
if(a=="SPACE"){document.write("");}
else if(a=="ADDcolor"){write_button_richtext(b, "加入顏色", "addcolor.gif", "ADDcolor");}
else if(a=="DELcolor"){write_button_richtext(b, "移除顏色", "delcolor.gif", "DELcolor");}
}
//--------------------------------------------------------------------------------------------------------------

function write_button_richtext(a,b,c,d) // 滑鼠移入按鈕的動作 //(代稱,解說,圖片,動作}
{
if(isSafari)
{document.write("");}
else
{document.write("");}
}
//--------------------------------------------------------------------------------------------------------------
function FormatText(a,b,c) //按鈕(滑鼠按下時的的動作) //(代稱, 動作, ''}
{
currenteditor=a;
var d;
if(isIE)
{
d=frames[a];
var f=d.document.selection;
if(f!=null){rng=f.createRange();}
}
else
{
d=document.getElementById(a).contentWindow;
var f=d.getSelection();
if(f!=""&&f.rangeCount>0)
{
rng=f.getRangeAt(f.rangeCount-1).cloneContents();
var g=d.document.createElement('div');
g.appendChild(rng);
}
}
if(b=="DELcolor")
{
if(isSafari)
{
d.document.execCommand("removeformat",false,c);
event.preventDefault();
event.returnValue=false;
}
else{d.document.execCommand("removeformat",false,c);}
}
else{parent.command="forecolor";}
}
//--------------------------------------------------------------------------------------------------------------
function SetFormat(data)
{
self.parent.SetFontFormat(data);
self.parent.close_insert_pop();
}
//--------------------------------------------------------------------------------------------------------------
function SetFontFormat(a,b)
{
var c=currenteditor;
var d;
if(isIE){d=frames[c];}
else{d=document.getElementById(c).contentWindow;}
var e=parent.command;
if(isIE)
{
var f=d.document.selection;
if(f!=null){var g=f.createRange();g=rng;g.select();}
}
else{d.focus();}
d.document.execCommand(e,false,a);
d.focus();
}
//--------------------------------------------------------------------------------------------------------------
var steditor=0;

document.onmouseup=new Function("dragapproved=false");

function close_insert_pop(){chkVK=0;document.getElementById("insert_pop").style.display="none";}

function NoError(){return(true);}
onerror=NoError;
//--------------------------------------------------------------------------------------------------------------
function WriteHTML(a,b) //常用字按鍵導引到輸入框
{
var c;
if(isIE)
{
c=frames[b];
c.focus();
var d=c.document.selection;
if(d!=null){rng=d.createRange();}
c.document.execCommand("removeformat",false,"");
rng.pasteHTML("");
rng.pasteHTML(a);
c.focus();
}
else if(isSafari)
{
c=document.getElementById(b).contentWindow;
c.focus();
a=a.replace(/ a=a.replace(/>/g,"[HOTAGCLOSE]");
c.document.execCommand('insertTEXT',false,a);
var e=document.getElementById(b).contentWindow.document.body.innerHTML;
e=e.replace(/\[HOTAGOPEN\]/g,'<');
e=e.replace(/\[HOTAGCLOSE\]/g,'>');
e=e.replace(/[\n\r]/ig,'');
document.getElementById(b).contentWindow.document.body.innerHTML=e;
c.focus();
}

else
{
c=document.getElementById(b).contentWindow;
c.focus();
c.document.execCommand('insertHTML',false," ");
c.document.execCommand("removeformat",false,"");
c.document.execCommand('insertHTML',false,a);
c.document.execCommand("removeformat",false,"");
c.focus();
}
}
//--------------------------------------------------------------------------------------------------------------

function htmlentities(a)
{
a=a.replace(/ a=a.replace(/>/g,'>');
a=a.replace(/&/g,'&');
return a;
}
//--------------------------------------------------------------------------------------------------------------

function BBCodeToHTML(a)
{
a=a.replace(/&/g,'&');
a=a.replace(/ a=a.replace(/>/g,'>');
a=a.replace(/ /g,' ');
a=a.replace(/\n/g,'
');
a=a.replace(/\[color=(.*?)\]/gi,'');
a=a.replace(/\[\/(font|color)\]/gi,'
');
return a;
}
//--------------------------------------------------------------------------------------------------------------

function HTMLToBBCode(a)
{
if(starup=="0")
{
a=a.replace(/]*>/gi,'\n');
a=a.replace(/]*)>/gi,'');
a=a.replace(/<\/p([^>]*)>/gi,'
\n');
}
else{a=htmlentities(a);}

var b=a.split("<");
var c=new Array();
var e=0;
if(b.length>1)
{
for(var i=0;i {
if(i>0)b[i]='<'+b[i];
var f=b[i];
if(f.match(/<(font|div)( ([^>]{1,}.*?)){0,1}( {0,1}){0,1}>/i))
{
var g=RegExp.$1;
var h=RegExp.$3;
if(h.toLowerCase().indexOf("style=")!=-1&&h.toLowerCase().indexOf("color:")!=-1&&h.toLowerCase().indexOf("color=")!=-1)
{h=h.replace(/color="(.*?)"/gi,"");}

h=h.replace(/(color=|style=)/ig,"|$1");
h=h.replace(/("|")/g,""); /////// h=h.replace(/('|")/g,"");
h=h.replace(/ \|/g,"|");
var j=h.split("|");
var k=new Array();
if(j!=null)
{
for(var z=0;z {
var l=j[z].split("=");
k[l[0].toLowerCase()]=j[z].replace(l[0].toLowerCase()+"=","");
}
}

var m='';
var g=g.toLowerCase();

if(g=="font")
{
if(j.length>0)
{
for(var r in k)
{
k[r]=k[r].replace(/^ +| +$/g,"");
if(r=="color"){m+='[color='+k['color']+']';}
else if(r=="style"){m+=AnalyzeHTMLBlock(g,k);}
}
}
}

else if(g=="div"||g=="span")
{
if(k['style']){m=AnalyzeHTMLBlock(g,k);}
else if(k['align']){m='['+k['align'].toUpperCase()+']';}
else{m='[HOTEDITOR_NEW_LINE]';}
}
b[i]=f.replace(/(<([^>]+)>)/,m);

if(g!="img"){c[e]=m;e++;}
}

else if(f.match(/<\/(font|div)>/i))
{
e--;
var w=c.pop();
if(w!=null)
{
var x="";
var A=w;
A=A.replace(/=(.*?)\]/g,"]");
A=A.replace(/\]/g,"],");
A=A.replace(/\[(.*?)\]/g,"[/$1]");
var B=A.split(",");
B.reverse();
for(var y=0;y x=x.replace(/\[\/\*\]/gi,"");
b[i]=b[i].replace(/(<([^>]+)>)/,x);
}
else{b[i]=b[i].replace(/(<([^>]+)>)/,"");}
}
}
var C=b.join("");
}

else{var C=a;}

C=C.replace(/ C=C.replace(/>/g,'>');
C=C.replace(/ /g,' ');
C=C.replace(/&/g,'&');
C=C.replace(/ /g,'\t');
C=C.replace(/\[HOTEDITOR_NEW_LINE\]/g,'\n');
C=C.replace(/\[\/HOTEDITOR_NEW_LINE\]\n+/g,'\n');
C=C.replace(/\[\/HOTEDITOR_NEW_LINE\]/g,'\n');

if(starup=="0")
{
C=C.replace(/\[\*\]/gi,'\n[*]');
C=C.replace(/\n\n\[\*\]/gi,'\n[*]');
}

C=C.replace(/\[color=#.\w*\]\[\/color\]/gi,"");
C=C.replace(/^\n+/,"");
C=C.replace(/\n+$/,"");
return C;
}
//--------------------------------------------------------------------------------------------------------------
function bbstyle(bbnumber)
{
if (bbnumber){
bbfontstyle(bbtags[bbnumber], '');
//document.forms[form_name].elements[text_name].focus();
}
return;
}
//--------------------------------------------------------------------------------------------------------------
function bbfontstyle(bbopen, bbclose)
{
theSelection = false;
var textarea = document.forms[form_name].elements[text_name];
textarea.focus();
if ((clientVer >= 4) && is_ie && is_win)
{
theSelection = document.selection.createRange().text;

if (theSelection)
{
document.selection.createRange().text = bbopen + theSelection + bbclose;
document.forms[form_name].elements[text_name].focus();
theSelection = '';
return;
}
}
else if (document.forms[form_name].elements[text_name].selectionEnd && (document.forms[form_name].elements[text_name].selectionEnd - document.forms[form_name].elements[text_name].selectionStart > 0))
{
document.forms[form_name].elements[text_name].focus();
theSelection = '';
return;
}

var caret_pos = getCaretPosition(textarea).start;
var new_pos = caret_pos + bbopen.length;
insert_text(bbopen + bbclose);
if (!isNaN(textarea.selectionStart)){textarea.selectionStart = new_pos;textarea.selectionEnd = new_pos;}
else if (document.selection)
{var range = textarea.createTextRange();range.move("character", new_pos);range.select();storeCaret(textarea);}
textarea.focus();
return;
}
//--------------------------------------------------------------------------------------------------------------
function insert_text(text, spaces, popup)
{
text = convert(text);
text = BBCodeToHTML(text);
WriteHTML(text,"editor");
}
//--------------------------------------------------------------------------------------------------------------
function caretPosition(){var start = null;var end = null;}
//--------------------------------------------------------------------------------------------------------------
function getCaretPosition(txtarea)
{
var caretPos = new caretPosition();

if(txtarea.selectionStart || txtarea.selectionStart == 0){caretPos.start = txtarea.selectionStart;caretPos.end = txtarea.selectionEnd;}
else if(document.selection)
{
var range = document.selection.createRange();
var range_all = document.body.createTextRange();
range_all.moveToElementText(txtarea);
var sel_start;
for (sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start++){range_all.moveStart('character', 1);}
txtarea.sel_start = sel_start;caretPos.start = txtarea.sel_start;caretPos.end = txtarea.sel_start;
}
return caretPos;
}


懇求諸位高手給予解答,謝謝~~~




  • 贊助網站       

    廣利不動產-新板特區指名度最高、值得您信賴的好房仲
    您的托付,廣利用心為您服務
    廣利不動產-板橋在地生根最實在--新板特區指名度最高、值得您信賴的好房仲
    完整房訊,房屋、店面熱門精選物件,廣利不動產 優質仲介,房屋租賃、買賣資訊透明,交易真安心!



  •  共 0 人回應

    姓名:
    佈告內容: