【问题标题】:how do I create a DOMSelection object from a specific node?如何从特定节点创建 DOMSelection 对象?
【发布时间】:2012-04-28 21:24:34
【问题描述】:

我的 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"

【问题讨论】:

  • 你能举个例子吗?

标签: javascript webkit dom-selection


【解决方案1】:

您是否正在寻找类似 document.getSelection() 的东西?我有点糊涂了,提前道歉。

例子:

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

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

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多