2013-05-27 60 views
0

我必須在CKEditor中的div標籤內創建一個span對話框窗口的onclick事件。我嘗試了下面的代碼,但它不工作。span標籤裏面的div標籤沒有出現

link = editor.document.createElement('div'); 
this.commitContent(data); 
link.setAttribute('itemscope',''); 
link.setAttribute('itemtype', 'http://schema.org/Person'); 
link.setAttribute('id', 'person'); 
link1 = editor.document.createElement('span'); 
document.getElementById("person").appendChild(link1); 
link1.setAttribute('itemprop', data.prop); 
+2

默認跨度不是'display:block',這意味着它不會佔用可用的空間,但會限制包裝內在的內容。所以,你需要指定'display:block'來顯示它。 – karthikr

+0

@karthikr,謝謝你的迴應,但它不起作用。出現此錯誤:TypeError:無法調用null的方法'appendChild' – Rabeel

+0

您嘗試了什麼? – karthikr

回答

1

您完全混淆了原生DOM API與CKEditor's API。甚至很難猜出你想要寫代碼的內容,但我希望這會對你有所幫助:

var link = editor.document.createElement('div'); 
this.commitContent(data); 
link.setAttributes({ 
    itemscope: '', 
    itemtype: 'http://schema.org/Person', 
    id: 'person' 
}); 
// Now you need to append link somewhere... 
editor.editable().append(link); 

var link1 = editor.document.createElement('span'); 
link1.appendTo(link); 
link1.setAttribute('itemprop', data.prop); 
+0

,這給了我這個錯誤:TypeError:對象#沒有方法'可編輯' – Rabeel

+0

所以你有其他東西壞了。編輯器具有'editable'方法。請參閱[文檔](http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-editable)。 PS。好的,我知道了 - 你正在使用CKEditor 3.可編輯是在CKEditor 4中引入的。所以用'editor.document.getBody()'替換'editor.editable()'。或者更好 - 升級CKEditor。 – Reinmar