Wysiwyg 执行没有选择的命令?(设置字体、大小等)

Wysiwyg 执行没有选择的命令?(设置字体、大小等),wysiwyg,execcommand,Wysiwyg,Execcommand,我可以让我的自定义WYSIWYG编辑器将样式应用于选定的文本没有问题: pnlDocumentEditor_IFrame.document.execCommand(cmd, ui, opt) 。。但我需要能够做的是允许用户设置字体、字号或粗体等,以便在发出此命令后键入的文本将应用该样式 这可能吗?我尝试过的所有execCommand似乎只对选定的文本起作用。在某些元素上应用execCommand,而不使用鼠标进行选择,可以通过以下功能完成: 来源:在某些元素上应用execCommand,而无需

我可以让我的自定义WYSIWYG编辑器将样式应用于选定的文本没有问题:

pnlDocumentEditor_IFrame.document.execCommand(cmd, ui, opt)
。。但我需要能够做的是允许用户设置字体、字号或粗体等,以便在发出此命令后键入的文本将应用该样式


这可能吗?我尝试过的所有execCommand似乎只对选定的文本起作用。

在某些元素上应用execCommand,而不使用鼠标进行选择,可以通过以下功能完成:


来源:

在某些元素上应用execCommand,而无需使用鼠标选择,可以使用此功能完成:


来源:

我感谢您的帮助,尽管在Safari下,这会增强整个字符串的勇气。我感谢您的帮助,尽管在Safari下,这会增强整个字符串的勇气。您找到解决方法了吗?没有选择,如何设置多种格式,然后键入并使用这些格式?恐怕没有。我们使用realobject.com editon NG editor。虽然是基于java的,但这是当时我能找到的最接近于在浏览器中替换完整单词的版本。你找到解决方法了吗?没有选择,如何设置多种格式,然后键入并使用这些格式?恐怕没有。我们使用realobject.com editon NG editor。虽然它是基于java的,但它是当时我能找到的最接近于在浏览器中替换完整单词的版本。
function execCommandOnElement(el, commandName, value) {
    if (typeof value == "undefined") {
        value = null;
    }

    if (typeof window.getSelection != "undefined") {
        // Non-IE case
        var sel = window.getSelection();

        // Save the current selection
        var savedRanges = [];
        for (var i = 0, len = sel.rangeCount; i < len; ++i) {
            savedRanges[i] = sel.getRangeAt(i).cloneRange();
        }

        // Temporarily enable designMode so that
        // document.execCommand() will work
        document.designMode = "on";

        // Select the element's content
        sel = window.getSelection();
        var range = document.createRange();
        range.selectNodeContents(el);
        sel.removeAllRanges();
        sel.addRange(range);

        // Execute the command
        document.execCommand(commandName, false, value);

        // Disable designMode
        document.designMode = "off";

        // Restore the previous selection
        sel = window.getSelection();
        sel.removeAllRanges();
        for (var i = 0, len = savedRanges.length; i < len; ++i) {
            sel.addRange(savedRanges[i]);
        }
    } else if (typeof document.body.createTextRange != "undefined") {
        // IE case
        var textRange = document.body.createTextRange();
        textRange.moveToElementText(el);
        textRange.execCommand(commandName, false, value);
    }
}
var testDiv = document.getElementById("test");
execCommandOnElement(testDiv, "Bold");
execCommandOnElement(testDiv, "ForeColor", "red");