在div中获取所选文本的html [英] get selected text's html in div

查看:430
本文介绍了在div中获取所选文本的html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div,其contentEditable设置为true。
我必须找到所选文本html。我能够在FireFox中获取所选文本

i have a div with contentEditable set to true. I have to find selected text html.I am able to get selected text in FireFox by

 window.getSelection();

我是IE的情况我可以使用

I case of IE i am able to get selected text html by using

document.selection.createRange().

但是,如何在FireFox中找到所选的文本html。
如何做到这一点。请帮助。

But, how can i find selected text html in FireFox. How can in do this.Please help.

推荐答案

选择文本并将其存储在名为<$ c $的变量中C> mytext的。

Select text and store it in variable called mytext.

if (!window.x) {
    x = {};
}
x.Selector = {};
x.Selector.getSelected = function() {
    var t = '';
    if (window.getSelection) {
        t = window.getSelection();
    } else if (document.getSelection) {
        t = document.getSelection();
    } else if (document.selection) {
        t = document.selection.createRange().text;
    }
    return t;
}

$(function() {
    $(document).bind("mouseup", function() {
        var mytext = x.Selector.getSelected();
        alert(mytext);
    });
});

这篇关于在div中获取所选文本的html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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