簡體   English   中英

contenteditable在文本范圍內選擇了哪些字母?

[英]contenteditable what chlidren are selected in text range?

所以我有一個帶有多個子段落的contenteditable div:

<div id="editor" contenteditable="true">
    <p>Paragraph1</p>
    <p>Paragraph2</p>
    <p>Paragraph3</p>
    <p>Paragraph4</p>
</div>

因此,使用window.getSelection()我可以選擇什么文本,我知道如果它是startContainer, startOffset ,則可以獲取范圍的開始位置,而endContainer, endOffset可以結束endContainer, endOffset位置的位置。 但是,如果我的選擇跨越多個子段落,那么我該如何判斷選擇了哪個子?

您可以使用以下答案中的函數,並過濾節點數組以僅包含<p>元素:

https://stackoverflow.com/a/7784176/96100

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM