how do I create a DOMSelection object from a specific node?
-
04-06-2021 - |
質問
My javascript is not that good so I can only hope this makes sense. I want to create a DOMSelection object like the one that gets returned from
window.getSelection()
from an onclick of the node. so lets say i have this jQuery listener setup like this:
$("#target").click(function() {
var sel = createSelectionFromNode(this);
});
I only want the range to be from the beginning of the node to the end of the node. Is there an easy way to do this? I am specifically looking for WebKit compatible solutions, it's ok to ignore IE. Thanks for any help.
Example: http://jsfiddle.net/8j4Bf/
in the example, clicking on the text should return a DOMSelection object like:
DOMSelection
anchorNode: Text
attributes: null
baseURI: "http://jsfiddle.net/8j4Bf/"
childNodes: NodeList[0]
data: "HTML"
firstChild: null
lastChild: null
length: 4
localName: null
namespaceURI: null
nextSibling: null
nodeName: "#text"
nodeType: 3
nodeValue: "HTML"
ownerDocument: HTMLDocument
parentElement: HTMLSpanElement
parentNode: HTMLSpanElement
prefix: null
previousSibling: null
textContent: "HTML"
wholeText: "HTML"
__proto__: Text
anchorOffset: 0
baseNode: Text
baseOffset: 0
extentNode: Text
extentOffset: 0
focusNode: Text
attributes: null
baseURI: "http://jsfiddle.net/8j4Bf/"
childNodes: NodeList[0]
data: "HTML"
firstChild: null
lastChild: null
length: 4
localName: null
namespaceURI: null
nextSibling: null
nodeName: "#text"
nodeType: 3
nodeValue: "HTML"
ownerDocument: HTMLDocument
parentElement: HTMLSpanElement
parentNode: HTMLSpanElement
prefix: null
previousSibling: null
textContent: "HTML"
wholeText: "HTML"
__proto__: Text
focusOffset: 0
isCollapsed: true
rangeCount: 1
type: "Caret"
正しい解決策はありません
他のヒント
Are you looking for something like document.getSelection() ? I'm a bit confused, so I apologize in advance.
Example:
所属していません StackOverflow