我想打一個活的文本編輯器,將允許在方括號中的所有文本應當強調的代碼編輯器直播。 [example]
作出這樣的格式文本和顏色
如何從編輯<div>
中提取文本並用代碼元素替換所有[text]
?
例如: 將[word]
替換爲<code>[word]</code>
。
這是我的問候編輯窗口:
<div contenteditable="true" id="edit">
The big <code>[color]</code> fox jumped over the lazy <code>[animal]</code>.
</div>
大[color]
狐狸跳過了懶惰[animal]
。
我做了JS的這一點,但它不會做任何事情:
EDIT = document.getElementById("edit");
EDIT.onkeypress = function(event) {
if (event.keyCode === 221) {
var text = EDIT.innerText;
EDIT.innerHTML = text.replace(/\[([\w\s]+)]/g, "<code>[$1]</code>");
}
};
正如你所看到的,我試圖用正則表達式的一點點做。
除此之外,我想不出辦法來完成我想要的東西。即使這樣也行不通。
謝謝。
你要問一個具體問題。這個網站太寬了。要按照這個原則來回答這個問題,就需要編寫一個大規模的教程,這對未來不太可能有幫助。 – Carcigenicate