获取 Selection() 对象中 <span> 个元素的列表
Getting list of <span> elements in a Selection() object
鉴于HTML喜欢:
<p>
<span id="text 1"> Hey everyone </span>
<span id="text 2"> I have a</span>
<span id="text 3"> question for you.</span>
</p>
以及从 "everyone" 到 "have" 的选择,我可以通过以下方式获取 Selection 对象:
function GetSelectedNodes() {
if (window.getSelection) {
var selection = window.getSelection();
var start_node = selection.anchorNode;
var end_node = selection.focusNode;
}
}
但我想要的是所选内容中 Span 元素的列表。我如何获得它?
window.getSelection().getRangeAt(0).cloneContents();
来源 - window.getSelection() gives me the selected text, but I want the HTML
鉴于HTML喜欢:
<p>
<span id="text 1"> Hey everyone </span>
<span id="text 2"> I have a</span>
<span id="text 3"> question for you.</span>
</p>
以及从 "everyone" 到 "have" 的选择,我可以通过以下方式获取 Selection 对象:
function GetSelectedNodes() {
if (window.getSelection) {
var selection = window.getSelection();
var start_node = selection.anchorNode;
var end_node = selection.focusNode;
}
}
但我想要的是所选内容中 Span 元素的列表。我如何获得它?
window.getSelection().getRangeAt(0).cloneContents();
来源 - window.getSelection() gives me the selected text, but I want the HTML