ContentEditable - 获取当前字体颜色/大小

发布于 2024-12-25 13:24:30 字数 155 浏览 4 评论 0原文

我正在为网络浏览器开发富文本编辑器,我想使用 RTE/ContentEditable 元素中当前字体颜色和大小的值。是否有一些预先选择的函数来获取这些值,例如 execCommand,它直接与 ContentEditable 元素连接?或者我应该使用一些文本范围 jQuery 库来获取这些属性?

I'm working on Rich Text Editor for web browser and I want to work with values of current font color and size in the RTE/ContentEditable element. Is there some preselected function to get these values, like execCommand, which is connected directly with ContentEditable element? Or should I use some text range jQuery library which will get these properties?

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

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

发布评论

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

评论(1

月棠 2025-01-01 13:24:30

您可以使用 document.queryCommandValue() 获取所有主要浏览器中当前选择的颜色和字体大小:

现场演示:http://jsfiddle.net/timdown/AJBsY/

代码:

var colour = document.queryCommandValue("ForeColor");
var fontSize = document.queryCommandValue("FontSize");

但是,不同浏览器的结果不一致,并且 FontSize 命令仅适用于 中使用的字体大小 1-7这HTML 元素而不是 CSS,因此您最好使用 window.getCompulatedStyle() 获取包含所选内容的元素并检查其 CSS 属性/ currentStyle

现场演示:http://jsfiddle.net/timdown/K4n2j/

代码:

function getComputedStyleProperty(el, propName) {
    if (window.getComputedStyle) {
        return window.getComputedStyle(el, null)[propName];
    } else if (el.currentStyle) {
        return el.currentStyle[propName];
    }
}

function reportColourAndFontSize() {
    var containerEl, sel;
    if (window.getSelection) {
        sel = window.getSelection();
        if (sel.rangeCount) {
            containerEl = sel.getRangeAt(0).commonAncestorContainer;
            // Make sure we have an element rather than a text node
            if (containerEl.nodeType == 3) {
                containerEl = containerEl.parentNode;
            }
        }
    } else if ( (sel = document.selection) && sel.type != "Control") {
        containerEl = sel.createRange().parentElement();
    }

    if (containerEl) {
        var fontSize = getComputedStyleProperty(containerEl, "fontSize");
        var colour = getComputedStyleProperty(containerEl, "color");
        alert("Colour: " + colour + ", font size: " + fontSize);
    }
}

这是一个改进,但仍然存在浏览器不一致的问题,例如不同的单位或颜色格式。

You can use document.queryCommandValue() to get the colour and font size of the current selection in all major browsers:

Live demo: http://jsfiddle.net/timdown/AJBsY/

Code:

var colour = document.queryCommandValue("ForeColor");
var fontSize = document.queryCommandValue("FontSize");

However, the results are inconsistent across browsers and the FontSize command only works with font sizes 1-7 used in the HTML <font> element rather than CSS, so you'd be better off getting hold of the element containing the selection and examining its CSS properties using window.getComputedStyle() / currentStyle:

Live demo: http://jsfiddle.net/timdown/K4n2j/

Code:

function getComputedStyleProperty(el, propName) {
    if (window.getComputedStyle) {
        return window.getComputedStyle(el, null)[propName];
    } else if (el.currentStyle) {
        return el.currentStyle[propName];
    }
}

function reportColourAndFontSize() {
    var containerEl, sel;
    if (window.getSelection) {
        sel = window.getSelection();
        if (sel.rangeCount) {
            containerEl = sel.getRangeAt(0).commonAncestorContainer;
            // Make sure we have an element rather than a text node
            if (containerEl.nodeType == 3) {
                containerEl = containerEl.parentNode;
            }
        }
    } else if ( (sel = document.selection) && sel.type != "Control") {
        containerEl = sel.createRange().parentElement();
    }

    if (containerEl) {
        var fontSize = getComputedStyleProperty(containerEl, "fontSize");
        var colour = getComputedStyleProperty(containerEl, "color");
        alert("Colour: " + colour + ", font size: " + fontSize);
    }
}

This is an improvement, but there are still browser inconsistencies such as differing units or colour formats.

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