我正在嘗試製作所見即所得的編輯器,至此我可以選擇文本並單擊「製作粗體」使所選文本變爲粗體。它實際上只是圍繞選定的文本包裝< b>(沒有空格)標籤。但我的問題是,如果我要取消大膽的是,我的劇本有一些麻煩......檢測文本是否爲粗體
到目前爲止,這裏是我的腳本:
<script language="javascript">
function format(tag) //defines function format
{
var editor = document.getElementById('editor');
var txt = '';
var tester = document.getElementById('tester');
if (window.getSelection) //if your browser uses this method of text selection
{
txt = window.getSelection();
}
else if (document.getSelection) //if your browser uses this method of text selection
{
txt = document.getSelection();
}
else if (document.selection) //if your browser uses this method of text selection
{
txt = document.selection.createRange().text;
}
else return; //Return this
matched = editor.innerHTML.match(txt); //Find the selected text in the editor
// if (matched.style.font-weight = "600") {tester.innerHTML = "already bold";} //if the selected text is bold, say 'already bold' DOES NOT WORK
// else {tester.innerHTML = "not bold";} //if it doesn't...
editor.innerHTML = editor.innerHTML.replace(matched,"<"+tag+">"+matched+"</"+tag+">");//Wrap <b> tags around it
}
</script>
<input type="button" value="Make Bold" onmousedown="format('b')">
<input type="button" value="Make Italic" onmousedown="format('i')">
<div id='editor' onclick="javascript:this.designMode='on';" designmode="on" contenteditable="true">Edit Box</div>
<span id="tester">testing span</span>
如果你嘗試一下,你可以輸入該文本框並選擇文本,然後單擊Make Bold(粗體),它將變爲粗體。現在再次點擊Make Bold,但沒有任何反應。它只是在選定的文本週圍添加另一個< b>標籤。我想讓它變得不大膽;正常。
我如何做到這一點?
謝謝:)
每個許可證下有100萬個所見即所得的編輯器可供您指定。除非你純粹作爲學習練習,否則我建議不要重蹈覆轍。 – mikerobi 2010-07-24 22:39:26