ユーザーが含まれるテキストを選択し<div></div>
て、太字のテキストに変更できるようにしています。つまり から<div>this is some text</div>
まで<div>this is <b>some</b> text</div>
。div.innerHTML を に変更すると、HTML としてレンダリングされて太字で表示されるのではなくthis is <b>some</b> text
、タグがユーザーに表示されることを除いて、すべてが機能しています。これはすべて Javascript を使用してクライアント側で行われます。<b>some</b>
タグをユーザーに公開するのではなく、ブラウザに強制的にレンダリングさせるにはどうすればよいですか?
リクエストごとに、ここにコードがあります...
HTML...
<div id="blob">
One simple, but not very efficient implementation of a dictionary is a linked
list. In this implementation all operations take linear time in the worst case
(and even in the average case), assuming that insertions first check whether the
item is in the current list. A more scalable implementation of a dictionary is a
balanced search tree. In this lecture note we present two even more efficient data
structures based on hashing.
</div>
ジャバスクリプト...
tagText(document.getElementById("blob"),"<b>","</b>");
と...
//======================================================================
function tagText(el,tagstart,tagend)
{
var range = window.getSelection().getRangeAt(0);
var rtxt = range.startContainer.textContent;
var rlen = rtxt.length;
var start = range.startOffset;
var stop = range.endOffset;
var result = rtxt.substring(0,start) + tagstart + rtxt.substring(start,stop) + tagend + rtxt.substring(stop,rlen);
// el.innerHTML = result;
range.startContainer.textContent = result;
var txt = el.innerHTML;
el.innerHTML = txt;
}
//======================================================================
firebug を介して div:innerHTML を見ると、タグが<b>
ではなくエスケープされていることがわかります<b>
。