gpt4 book ai didi

javascript - 使用javascript选定文本的背景颜色

转载 作者:行者123 更新时间:2023-11-30 17:43:06 25 4
gpt4 key购买 nike

我有一个带有 contenteditable="true"的 div,我想获得所选文本的背景颜色。它在 Chrome 中运行良好,但在 Firefox 中一直返回“透明”而失败。这是我现在尝试做的。HTML:

<div contenteditable="true" id="selector" style="background-color: rgb(255,255,0);">
Test back<span style="background-color: rgb(255,0,0);">ground</span> color.
</div>

Javascript

$('div#selector').on('mouseup', function (){
alert(document.queryCommandValue('backColor'));
});

样本:http://jsfiddle.net/4Wk2X/11/

你知道为什么会这样吗?

最佳答案

我让它像这样工作:

$('div#selector').on('mouseup', function (){
alert($(this).css('color'));
});

参见 fiddle

关于javascript - 使用javascript选定文本的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20663742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com