0

我的 javascript 不是那么好,所以我只能希望这是有道理的。我想创建一个 DOMSelection 对象,就像从

window.getSelection()

从节点的点击。所以可以说我有这样的jQuery监听器设置:

$("#target").click(function() {
    var sel = createSelectionFromNode(this);
});

我只希望范围是从节点的开头到节点的结尾。是否有捷径可寻?我专门找WebKit兼容的解决方案,可以忽略IE。谢谢你的帮助。

示例:http: //jsfiddle.net/8j4Bf/

在示例中,单击文本应返回一个 DOMSelection 对象,如:

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"
4

1 回答 1

0

你在寻找类似 document.getSelection() 的东西吗?我有点困惑,所以我提前道歉。

例子:

http://jsfiddle.net/8j4Bf/1/

https://developer.mozilla.org/en/DOM/document.getSelection

于 2012-04-28T23:33:21.003 回答