如何利用js取得eWebEditor編輯器的內容

用javascript取控件的值原本是一件簡單的事卻被eWebEditor搞的很不方便,致使不少初學者,不知道該如何獲取。在分析以前先把咱們習慣性的取值方法寫出來。 javascript

[html] view plain copy
  1. <HTML>  
  2.   
  3. <HEAD>  
  4.   
  5. <TITLE>eWebEditor : 標準調用示例</TITLE>  
  6.   
  7. <META http-equiv=Content-Type content="text/html; charset=gb2312">  
  8.   
  9. <link rel='stylesheet' type='text/css' href='example.css'>  
  10.   
  11. <script>  
  12.   
  13.     function validateForm(){  
  14.   
  15.   
  16.         if(document.getElementById("content1").value!=""){  
  17.   
  18.             document.getElementById("myform").submit();  
  19.   
  20.         }else{  
  21.   
  22.             alert("空");  
  23.   
  24.         }  
  25.   
  26.     }  
  27.   
  28. </script>  
  29.   
  30. </HEAD>  
  31.   
  32. <BODY>  
  33.   
  34. <FORM method="post" name="myform" action="rs.jsp">  
  35.   
  36. <TABLE border="0" cellpadding="2" cellspacing="1">  
  37.   
  38. <TR>  
  39.   
  40.     <TD>編輯內容:</TD>  
  41.   
  42.     <TD>  
  43.   
  44.         <INPUT type="hidden" name="content1" >  
  45.   
  46.         <IFRAME ID="eWebEditor1" src="../ewebeditor.htm?id=content1&style=coolblue" frameborder="0" scrolling="no" width="550" height="350"></IFRAME>  
  47.   
  48.     </TD>  
  49.   
  50. </TR>  
  51.   
  52. <TR>  
  53.   
  54.     <TD colspan=2 align=right>  
  55.   
  56.     <INPUT type=button value="提交" onclick="validateForm()">   
  57.   
  58.     <INPUT type=reset value="重填">   
  59.   
  60.     <INPUT type=button value="查看源文件" onclick="location.replace('view-source:'+location)">   
  61.   
  62.     </TD>  
  63.   
  64. </TR>  
  65.   
  66. </TABLE>  
  67.   
  68. </FORM>  
  69.   
  70. </BODY>  
  71.   
  72. </HTML>  

 

上面代碼很是簡單咱們通常會認爲document.getElementById("content1").value這樣就能夠取值了,但事實上 並非這樣,經過這種方式取值,只能取到初始值,當編輯器的內容變化時是取不到的,爲何呢?爲何後臺程序能夠取獲得編輯器中的值 呢,<%=request.getParameter("content1")%>這裏是能夠取到編輯器中的內容的,可是 document.getElementById("content1").value確不能夠。看來eWebEditor在js中動了手腳,必定是動態 幫定了提交事件,或動態綁定了在源碼中搜索onsubmit找到以下代碼,原來動態的綁定了onsubmit事件,這樣每次在提交前會執行 AttachSubmit函數 css

[jscript] view plain copy
  1. oForm.attachEvent("onsubmit", AttachSubmit) ;  
  2.   
  3. if (! oForm.submitEditor) oForm.submitEditor = new Array() ;  
  4.   
  5. oForm.submitEditor[oForm.submitEditor.length] = AttachSubmit ;  
  6.   
  7. if (! oForm.originalSubmit) {  
  8.   
  9.     oForm.originalSubmit = oForm.submit ;  
  10.   
  11.     oForm.submit = function() {  
  12.   
  13.         if (this.submitEditor) {  
  14.   
  15.             for (var i = 0 ; i < this.submitEditor.length ; i++) {  
  16.   
  17.                 this.submitEditor[i]() ;  
  18.   
  19.             }  
  20.   
  21.         }  
  22.   
  23.         this.originalSubmit() ;  
  24.   
  25.     }  
  26.   
  27. }  
[jscript] view plain copy
  1. function AttachSubmit() {   
  2.   
  3.     var oForm = oLinkField.form ;  
  4.   
  5.     if (!oForm) {return;}  
  6.   
  7.       
  8.   
  9.     var html = getHTML();  
  10.   
  11.     ContentEdit.value = html;  
  12.   
  13.     if (sCurrMode=="TEXT"){  
  14.   
  15.         html = HTMLEncode(html);  
  16.   
  17.     }  
  18.   
  19.     splitTextField(oLinkField, html);  
  20.   
  21. }  

AttachSubmit就是copy編輯器的內容到隱藏域控件中的過程。 html

知道了過程咱們的問題就不難解決了。只需在取編輯器內容以前執行下AttachSubmit便可 java

[jscript] view plain copy
  1. function validateForm(){  
  2.   
  3.     window.frames["eWebEditor1"].AttachSubmit();//執行iframe頁面中的AttachSubmit函數  
  4.   
  5.     if(document.getElementById("content1").value!=""){  
  6.   
  7.         document.getElementById("myform").submit();  
  8.   
  9.     }else{  
  10.   
  11.         alert("空");  
  12.   
  13.     }  
  14.   
  15. }  

 整個過程就此結束,其實eWebEditor代碼中的不少思想都是很具備參考價值的例如AttachSubmit的綁定submit 方法的從新封裝,我還發現一段比較寫的比較好的代碼也一塊兒貼出來。 web

[jscript] view plain copy
  1. var URLParams = new Object() ;  
  2.   
  3. var aParams = document.location.search.substr(1).split('&') ;  
  4.   
  5. for (i=0 ; i < aParams.length ; i++) {  
  6.   
  7.     var aParam = aParams[i].split('=') ;  
  8.   
  9.     URLParams[aParam[0]] = aParam[1] ;  
  10.   
  11. }  
  12.   
  13.   
  14.   
  15. var sLinkFieldName = URLParams["id"] ;  
  16.   
  17. var sExtCSS = URLParams["extcss"] ;  
  18.   
  19. var sFullScreen = URLParams["fullscreen"];  
  20.   
  21.   
  22.   
  23. var config = new Object() ;  
  24.   
  25. config.StyleName = (URLParams["style"]) ? URLParams["style"].toLowerCase() : "coolblue";  
  26.   
  27. config.CusDir = URLParams["cusdir"];  
  28.   
  29. config.ServerExt = "jsp";  

 

解析url的方法,這種方法之前koko跟我說過一回,今天在ewebeditor中又看到了,看來是一種比較常規的分析URL參數的方法。 jsp

總結:其實eWebEditor只是修改了提交表單的兩個事件,在提交表單前進行值copy,從而避免了編輯器每次更新都同步值這種沒有必要的操做。 編輯器

相關文章
相關標籤/搜索