2014-05-08 41 views
0

我需要將Salesforce提供的富文本編輯器的spellcheck屬性設置爲true。但是,指定屬性的主體組件始終返回null。CKEditor正文返回null時的Document.getElementById

由於這個原因,我無法更改屬性值。我究竟做錯了什麼?我甚至無法使用相同的jQuery選擇器。

<apex:page > 
    <apex:form > 
    <apex:inputTextarea richText="true" id="rta"/> 
    </apex:form> 
    <script> 
    alert(document.getElementById('j_id0:j_id1:rta_rta_body')); 
    </script> 
</apex:page> 

回答

0

現在太快了。

遇到此行代碼時,此腳本立即觸發。那時原來的<textarea>仍然存在於頁面上,並且裝飾器沒有運行(修飾器隱藏了原始標籤,而是用整個RTF編輯器注入)。您可以驗證它,因爲執行停止時它顯示此alert()

最乾淨的方法是重寫的onload函數(或者是任何一個是裝飾運行)相似,這些職位是如何做到這一點:

  1. http://bobbuzzard.blogspot.co.uk/2011/05/onload-handling.html
  2. https://developer.salesforce.com/forums?id=906F0000000957DIAQ

不知道爲什麼,但似乎即使這樣的自定義onload過早(我沒有時間進一步調試)。

因此,醜陋的,因爲它是 - 這似乎爲我工作:

<apex:page > 
    <apex:form > 
     <apex:inputTextarea richText="true" id="rta"/> 
    </apex:form> 
    <script> 
    function setSpellChecker(){ 
     var iframe = document.getElementById('j_id0:j_id1:rta_frame'); 
     var iframeDocument = iframe.contentDocument || iframe.contentWindow.document; 
     iframeDocument.getElementsByTagName('body')[0].spellcheck = true; 
    } 
    window.setTimeout(setSpellChecker, 1000); // fire after 1 second 
    </script> 
</apex:page> 

隨時實驗,使用jQuery的contents()美化它,如果你想...有很多在這裏是如何的問題用jQuery很好地訪問iframe,例如How to get the body's content of an iframe in Javascript?

相關問題