我使用鼠標在html頁面(在firefox中打開)上選擇一些文本,并使用javascript函數(shù)創(chuàng)建/獲取與所選文本相對應(yīng)的rangeobject。 userSelection =window.getSelection(); var rangeObject = getRangeObject(userSelection);現(xiàn)在我要突出顯示rangeobject下的所有文本。 var span = document.createElement("span"); rangeObject.surroundContents(span); span.style.backgroundColor = "yellow";好吧,這僅在rangeobject(起點和終點)位于同一textnode時才有效,然后突出顯示相應(yīng)的文本。 <p>In this case,the text selected will be highlighted properly, because the selected text lies under a single textnode</p>但是,如果range對象覆蓋多個文本節(jié)點,則它不能正常工作,它僅突出顯示第一個textnode中的文本,例如 <p><h3>In this case</h3>, only the text inside the header(h3) will be highlighted, not any text outside the header</p> 我不知道如何確定范圍對象下的所有文本,突出顯示范圍范圍是單個節(jié)點還是多個節(jié)點?謝謝....
如何突出顯示DOM Range對象的文本?
瀟湘沐
2019-10-11 10:14:01