没有alt属性和脚本的getSelection吗? [英] getSelection without alt attribute and scripts in it?

查看:95
本文介绍了没有alt属性和脚本的getSelection吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用window.getSelection()获取选定的文本. 但是,如果我也选择图像,它也会返回图像的全部.

I'm using window.getSelection () to get the selected text. But, if i select an image too, it returns also altof an image.

示例:

<img src="someSrc.jpg" alt="image_alt" /> My text here ... 

如果我也选择图像,它将返回

if i select an image too, it returns

image_alt我的文字在这里...

image_alt My text here ...

但是我只需要

我的文字在这里...

My text here ...

有什么方法可以只获取文本,而没有alt?

Is there any way to get only text, without alt?

非常感谢

推荐答案

最简单的方法是使用选择范围的toString()方法,这是在当前版本中指定window.getSelection().toString()执行的操作 WHATWG的新功能范围规范(尽管这是与某些浏览器的操作相反,并且可能会更改,也可能不会更改).以下内容适用于多个选定范围(Mozilla支持),也适用于IE< 9.

The easiest way would be to use the toString() method of selection's Range(s), which is what window.getSelection().toString() is specified to do in the current version of WHATWG's new Range spec (although this is contrary to what some browsers do and may or may not change). The following will work with multiple selected ranges (which Mozilla supports) and also in IE < 9.

jsFiddle: http://jsfiddle.net/timdown/HkP2S/

jsFiddle: http://jsfiddle.net/timdown/HkP2S/

代码:

function getSelectionRangeText() {
    var selText = "";
    if (window.getSelection) {
        var sel = window.getSelection(), rangeCount = sel.rangeCount;
        if (rangeCount) {
            for (var i = 0, rangeTexts = []; i < rangeCount; ++i) {
                rangeTexts.push("" + sel.getRangeAt(i));
            }
            selText = rangeTexts.join("");
        }
    } else if (document.selection && document.selection.type == "Text") {
        selText = document.selection.createRange().text;
    }
    return selText;
}

更新

此解决方案在<script><style>元素内包含文本.要删除此错误,可以在选择范围上使用cloneContents()并遍历所得文档片段的DOM,仅从<script><style>元素中未包含的文本节点收集文本.您还可以对此进行扩展,以删除CSS display: none元素内部的文本.

This solution includes text inside <script> and <style> elements. To remove this, you could use cloneContents() on the selection ranges and traverse the DOM of the resulting document fragments, collecting text only from text nodes not contained within <script> and <style> elements. You could also expand on this to remove text that is inside elements with CSS display: none.

jsFiddle: http://jsfiddle.net/timdown/HkP2S/2/

jsFiddle: http://jsfiddle.net/timdown/HkP2S/2/

代码:

function getSelectionRangeText() {
    var selText = "", selTextParts = [];

    function getNodeText(node) {
        if (node.nodeType == 3) {
            selTextParts.push(node.data);
        } else if (node.hasChildNodes()
        && !(node.nodeType == 1 && /^(script|style)$/i.test(node.tagName))) {
            for (var child = node.firstChild; !!child; child = child.nextSibling) {
                getNodeText(child);
            }
        }
    }

    if (window.getSelection) {
        var sel = window.getSelection(), rangeCount = sel.rangeCount;
        if (rangeCount) {
            for (var i = 0; i < rangeCount; ++i) {
                getNodeText(sel.getRangeAt(i).cloneContents());
            }
            selText = selTextParts.join("");
        }
    } else if (document.selection && document.selection.type == "Text") {
        selText = document.selection.createRange().text;
    }
    return selText;
}

这篇关于没有alt属性和脚本的getSelection吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆