我可以使用JQuery获得突出显示的文本吗? [英] Can I get highlighted text with JQuery?

查看:127
本文介绍了我可以使用JQuery获得突出显示的文本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我什么都没找到。是否有JQuery解决方案来检索突出显示的文本?我需要检查突出显示的文本是否有跨度,获取这些跨度的样式属性,并根据它进行操作。我可以用正则表达式做任何明显的事情,但首先我需要访问突出显示的文本!我很难找到一个跨浏览器的普通javascript解决方案,所以如果你有一个方便的,也会有很大的帮助。

I'm not finding anything anywhere. Is there a JQuery solution to retrieve highlighted text? I need to check the highlighted text for spans, get those spans' style attributes, and manipulate them based on that. I can do that part with regular expressions or whatever obviously, but first I need access to the highlighted text! I'm struggling to even find a cross-browser plain javascript solution, so if you've got one of those handy that would help immensely as well.

谢谢!

推荐答案

你的意思是用鼠标选择文字,所以在这里查看DEMO http://jsfiddle.net/yeyene/GYuBv/2/

You mean text selection with mouse, so check here, DEMO http://jsfiddle.net/yeyene/GYuBv/2/

$('#showSelected').on('click', function(){

    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }

    alert(text);       
});

如果要对所选文本进行一些更改,请检查此演示 http://jsfiddle.net/yeyene/GYuBv/3/

If you want to do some changes around selected text, check this DEMO http://jsfiddle.net/yeyene/GYuBv/3/

这篇关于我可以使用JQuery获得突出显示的文本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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