getSelection 中没有 alt 属性和脚本吗?

发布于 2024-12-02 07:13:52 字数 361 浏览 0 评论 0原文

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

示例:

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

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

image_alt 我的文字在这里...

但我只需要

我的文字在这里...

有什么方法可以只获取文字,没有替代品吗?

非常感谢

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

EXAMPLE:

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

if i select an image too, it returns

image_alt My text here ...

But i need only

My text here ...

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

Thanks much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昵称有卵用 2024-12-09 07:13:53

试试这个:

window.getTextSelection = function() {
    //First get HTML Fragment of selection
    var html = window.getSelection().getRangeAt(0).cloneContents(); 
    //Return only the text
    return html.textContent||html.innerText;
}

在某些情况下,您可以简单地通过 CSS 禁用用户选择:
您也可以通过禁用图像的用户选择来实现此目的:

img {
        -webkit-user-select: none;
        -khtml-user-select: none;
        -moz-user-select: none;
        -o-user-select: none;
        user-select: none;
}

Try this:

window.getTextSelection = function() {
    //First get HTML Fragment of selection
    var html = window.getSelection().getRangeAt(0).cloneContents(); 
    //Return only the text
    return html.textContent||html.innerText;
}

In some cases you can simply disable the user selection via CSS:
May you also can achieve this by disabling user-select for images:

img {
        -webkit-user-select: none;
        -khtml-user-select: none;
        -moz-user-select: none;
        -o-user-select: none;
        user-select: none;
}
北城挽邺 2024-12-09 07:13:53

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

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;
}

更新

此解决方案包括

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;
}

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/

Code:

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;
}

UPDATE

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/

Code:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文