简体   繁体   English

Javascript突出显示所选范围按钮

[英]Javascript Highlight Selected Range Button

I'm attempting to create a study tool for a page that allows a user to select any text on the page and click a button. 我正在尝试为页面创建一个学习工具,允许用户选择页面上的任何文本并单击按钮。 This click then formats the selected text with a yellow background. 然后,此单击将使用黄色背景格式化所选文本。 I can make this work inside of a single tag, but if the range of selection is across multiple tags (for instance, the first LI in an unordered list along with half of the second), I have difficulty applying the style. 我可以在单个标签内部进行此工作,但如果选择范围跨越多个标签(例如,无序列表中的第一个LI以及第二个的一半),我将难以应用该样式。 I can't just wrap the selection with a span here unfortunately. 不幸的是,我不能在这里用一个跨度包装选择。

Basically, I want the effects associated with contentEditable and execCommand without actually making anything editable on the page except to apply a background color to the selected text with the click of a button. 基本上,我想要与contentEditableexecCommand相关联的效果,而不实际在页面上进行任何可编辑的操作,只需单击按钮将背景颜色应用于所选文本。

I'm open to jQuery solutions, and found this plug-in that seems to simplify the ability to create ranges across browsers, but I was unable to use it to apply any formatting to the selected range. 我对jQuery解决方案持开放态度,并发现这个插件似乎简化了跨浏览器创建范围的能力,但我无法使用它来将任何格式应用于所选范围。 I can see from the console that it's picking up on the selection, but using something like: 我可以从控制台看到它正在接受选择,但使用类似的东西:

var selected = $().selectedText();
$(selected).css("background-color","yellow");

has no effect. 没有效果。

Any help pointing me in the right direction would be greatly appreciated. 任何指导我正确方向的帮助将不胜感激。

The following should do what you want. 以下应该做你想要的。 In non-IE browsers it turns on designMode , applies a background colour and then switches designMode off again. 在非IE浏览器中,它打开designMode ,应用背景颜色,然后再次关闭designMode

UPDATE UPDATE

Fixed to work in IE 9. 修复了在IE 9中工作。

function makeEditableAndHighlight(colour) {
    sel = window.getSelection();
    if (sel.rangeCount && sel.getRangeAt) {
        range = sel.getRangeAt(0);
    }
    document.designMode = "on";
    if (range) {
        sel.removeAllRanges();
        sel.addRange(range);
    }
    // Use HiliteColor since some browsers apply BackColor to the whole block
    if (!document.execCommand("HiliteColor", false, colour)) {
        document.execCommand("BackColor", false, colour);
    }
    document.designMode = "off";
}

function highlight(colour) {
    var range, sel;
    if (window.getSelection) {
        // IE9 and non-IE
        try {
            if (!document.execCommand("BackColor", false, colour)) {
                makeEditableAndHighlight(colour);
            }
        } catch (ex) {
            makeEditableAndHighlight(colour)
        }
    } else if (document.selection && document.selection.createRange) {
        // IE <= 8 case
        range = document.selection.createRange();
        range.execCommand("BackColor", false, colour);
    }
}

As far as I know, you won't be able to apply styling to regular text. 据我所知,您将无法将样式应用于常规文本。 You some sort of html element to operate on. 你可以使用某种html元素进行操作。 You could try wrapping the selected text with a span and then style the span. 您可以尝试使用范围包装所选文本,然后设置范围样式。

$().selectedText().wrap("<span></span>").parent().addClass("wrapped").css({backgroundColor: "Yellow"});

I added a class of "wrapped" as well, so that on subsequent attempts to highlight text, you can remove previous highlights. 我还添加了一个“包装”类,以便在后续尝试突出显示文本时,可以删除以前的突出显示。

$(".wrapped").each(function(){ ($(this).replaceWith( $(this).text() });

Code is untested. 代码未经测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM