gpt4 book ai didi

javascript - 获取点击文本的字体颜色?

转载 作者:行者123 更新时间:2023-11-28 14:53:05 24 4
gpt4 key购买 nike

下面的示例将在您双击选择“Here”一词时获取该词的颜色。当您只需在其中单击一次时,我需要获取颜色。我还需要一种方法来获取没有 <font color=""></font> 的文本的颜色它周围的标签,也许是RGB值?基本上,当您单击字符串上的任意位置时,我需要获取您单击的单词的字体颜色。如何实现这一点?

https://jsfiddle.net/7ng9j49t/

HTML:

<div contentEditable="true"><font color="gold">Here</font> is some text.</div>

JS:

$('div').mousedown(function() {
var selected = window.getSelection().focusNode.parentNode;
var color = $(selected).attr('color');
console.log(color);
});

更新:将 event.target 与 css('color') 一起使用按我的预期工作: https://jsfiddle.net/fof2u7L9/

$('div').mousedown(function(event) {
var clicked = event.target;
var color = $(clicked).css('color');
console.log(color);
});

最佳答案

  • <font>标签已弃用。
  • 要获得绝对颜色,请使用 window.getComputedStyle() :

代码:

var selected = /* get selected element */;
var color = window.getComputedStyle(selected).getPropertyValue("color");

关于javascript - 获取点击文本的字体颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43855454/

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