![](/img/trans.png)
[英]Gmail body returning null with document.getElementById(“”)
[英]Document.getElementById on CKEditor body returning null
我需要將Salesforce提供的富文本編輯器的spellcheck屬性設置為true。 但是,在其上指定屬性的主體組件始終返回null。
因此,我無法更改屬性值。 我究竟做錯了什么? 我什至不能使用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>
還為時過早。
一旦遇到此行代碼,就會觸發此腳本。 那時原始的<textarea>
仍然存在於頁面上,裝飾器沒有運行(裝飾器隱藏原始標簽,而是使用整個RTF編輯器注入<iframe>
)。 您可以驗證它,因為在顯示此alert()
執行停止了。
最干凈的方法是重寫onload函數(或裝飾器在任何地方運行),類似於這些帖子的工作方式:
不知道為什么,但是似乎這樣的自定義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,例如, 如何在Javascript中獲取iframe的主體內容?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.