Lil*_*ilz 19 javascript webpage document window paragraph
突出显示文本后,我想获取所选文本所在的段落.
var select = window._content.document.getSelection();
Run Code Online (Sandbox Code Playgroud)
有什么指针吗?
cle*_*tus 20
这实际上很难做到,因为你必须考虑六种情况:
首先,您必须决定解决方案的完整程度.我只会介绍(1)和(2)中最简单的情况.
function getSelectedParagraphText() {
if (window.getSelection) {
selection = window.getSelection();
} else if (document.selection) {
selection = document.selection.createRange();
}
var parent = selection.anchorNode;
while (parent != null && parent.localName != "P") {
parent = parent.parentNode;
}
if (parent == null) {
return "";
} else {
return parent.innerText || parent.textContent;
}
}
Run Code Online (Sandbox Code Playgroud)
注意:如果您在标签之后也将textContent替换为innerHTML.
编辑:更好的版本,包括更好的浏览器兼容性.
| 归档时间: |
|
| 查看次数: |
18345 次 |
| 最近记录: |