在查詢clientRects(getClientRects)的範圍時,在webkit瀏覽器(在IE9或Mozilla中這似乎不是問題),返回的clientrects包含對應文本的矩形到選定的文本,還包括範圍內任何元素的開始的客戶端反饋。我如何確定哪些客戶端產品屬於這些元素。webkit瀏覽器中來自getClientRects的clientRect的語義
比如讓說,這對用相同的換行符渲染我這裏有:
<p>This is some text StartSelection in a paragraph
blah blah<span>this is some EndOfSelection blah
more text</span>
</p>
如果我選擇從StartSelection到EndOfSelection,然後查詢從選擇範圍 - (0)
getRangeAt我還是會回到這個數組clientRects的:
0. rect of 'StartSelection in a paragraph'
1. rect of 'blah blah'
2. rect of 'this is some EndOfSelection blah'
3. rect of 'more text'
4. rect of 'this is some EndOfSelection'
唯一rects我想是0,1,& 4 Rects 2 & 3代表選擇交叉元素的開始標記的內容。
對矩形的幾何檢查可以讓我放棄2 & 3,但是代碼讓我覺得有點脆弱,特別是如果我的選擇延伸到第三條線的話。無論如何確定沒有幾何檢查的矩形的來源,我可以用它來丟棄2 & 3?
出於好奇,你想用'getClientRects()'實現什麼? –
我試圖突出顯示文本,與文本選擇分開。如果我可以在選擇中抽取多個範圍,我可以忍受這一點,但似乎支持有限。 – freddylindstrom
爲什麼不走Range的DOM或使用'document.execCommand()'(例如:http://stackoverflow.com/questions/6972581/javascript-quandary-creating-a-highlighter-pen-tool-almost-there/6972694 #6972694)? –