I have the following problem. I have to find a phrase in html document and surround each word in the phrase in a span element. I'm searching with this code:
var range = body.createTextRange();
range.moveToElementText(body);
var i = 0;
do
{
range.findText(note.Text, range.text.Length, 0);
i++;
if (i < note.Occurence)
range.moveStart("character", 1);
} while (i != note.Occurence);
Then I surround every word in range.text with a span element and I try to replace original phrase with the one surrounded by spans.
Everything works fine if the phrase contains only one or a few words, but when the phrase is big, my spans are not inserted in html at all. for example, when I try to paste the following html in range:
<span id='0' style='background-color:gray' class='Annotation'>?????</span> <span id='0' style='background-color:gray' class='Annotation'>????????</span> <span id='0' style='background-color:gray' class='Annotation'>???????</span> <span id='0' style='background-color:gray' class='Annotation'>??</span> <span id='0' style='background-color:gray' class='Annotation'>?????????????</span> <span id='0' style='background-color:gray' class='Annotation'>????</span> <span id='0' style='background-color:gray' class='Annotation'>?????????</span> <span id='0' style='background-color:gray' class='Annotation'>????</span> <span id='0' style='background-color:gray' class='Annotation'>???????????????????,</span>
<span id='0' style='background-color:gray' class='Annotation'>??????,</span> <span id='0' style='background-color:gray' class='Annotation'>???</span> <span id='0' style='background-color:gray' class='Annotation'>????????</span> <span id='0' style='background-color:gray' class='Annotation'>??</span> <span id='0' style='background-color:gray' class='Annotation'>???????????,</span> <span id='0' style='background-color:gray' class='Annotation'>??????</span> <span id='0' style='background-color:gray' class='Annotation'>???????</span> <span id='0' style='background-color:gray' class='Annotation'>????????</span> <span id='0' style='background-color:gray' class='Annotation'>??</span>
<span id='0' style='background-color:gray' class='Annotation'>??????????,???????????</span> <span id='0' style='background-color:gray' class='Annotation'>??</span> <span id='0' style='background-color:gray' class='Annotation'>??????????</span> <span id='0' style='background-color:gray' class='Annotation'>????????????</span> <span id='0' style='background-color:gray' class='Annotation'>??????????</span> <span id='0' style='background-color:gray' class='Annotation'>????????</span>
<span id='0' style='background-color:gray' class='Annotation'>???????????</span> <span id='0' style='background-color:gray' class='Annotation'>???????</span> <span id='0' style='background-color:gray' class='Annotation'>???????????</span> <span id='0' style='background-color:gray' class='Annotation'>????????</span> <span id='0' style='background-color:gray' class='Annotation'>??????????</span> <span id='0' style='background-color:gray' class='Annotation'>?????.</span> <span id='0' style='background-color:gray' class='Annotation'>??</span> <span id='0' style='background-color:gray' class='Annotation'>??????????</span> <span id='0' style='background-color:gray' class='Annotation'>????</span>
<span id='0' style='background-color:gray' class='Annotation'>??????????</span> <span id='0' style='background-color:gray' class='Annotation'>?????</span> <span id='0' style='background-color:gray' class='Annotation'>?????????????????</span> <span id='0' style='background-color:gray' class='Annotation'>?????????</span> <span id='0' style='background-color:gray' class='Annotation'>??????</span> <span id='0' style='background-color:gray' class='Annotation'>?????</span> <span id='0' style='background-color:gray' class='Annotation'>??????,</span> <span id='0' style='background-color:gray' class='Annotation'>??????</span>
<span id='0' style='background-color:gray' class='Annotation'>?????????</span> <span id='0' style='background-color:gray' class='Annotation'>??????????</span>
span elements are stripped off from the document and I get plain text. Any idea why is this happening?
Thanks.
UPDATE
After doing some more tests, looks like passing existing html (without modification) markup to pasteHtml breaks the output (and inserts <br>
tags for unknown reason).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…