Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
141 views
Welcome To Ask or Share your Answers For Others

1 Answer

Looks like pasteHtml is a bit buggy. I found the solution by acquiring range parent element via range.parentElement() and manipulating it's innerHtml (replace, append etc).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...