简体   繁体   English

样式跨度textnode没有样式子节点

[英]Styling span textnode without styling child nodes

So I'm creating a theme plugin for a forum where users often enter text with custom coloring. 所以我正在为一个论坛创建一个主题插件,用户经常输入带有自定义着色的文本。 This is implemented as an inline style on a span. 这是作为跨度上的内联样式实现的。

The forum by default has a dark color scheme so most of the text is light. 默认情况下,论坛采用深色配色方案,因此大部分文字都很清晰。 If I create a theme with a light color scheme this text would be hard to see. 如果我使用浅色方案创建主题,则很难看到该文本。

I thought of using a CSS5 color filter that targets text with inline colors: 我想过使用一个CSS5滤色器,它使用内联颜色来定位文本:

.Comment span[style^=color] {
   filter: hue-rotate(180deg) invert(100%);
   -webkit-filter: hue-rotate(180deg) invert(100%);
}

By inverting and rotating the color spectrum this turns a light blue color into a dark blue and light red into dark red, preserving the hue but making it darker. 通过反转和旋转色谱,这将浅蓝色变成深蓝色,浅红色变成深红色,保持色调但使其变暗。 This actually works but it has the side effect of also inverting the colors of images and other elements embedded in the text. 这实际上有效但它具有反转图像颜色和文本中嵌入的其他元素的副作用。

I thought of doing another color inversion on child elements but this leaves images looking like junk since apparently hue-rotate is not very accurate at all. 我想在子元素上进行另一种颜色反转,但这会使图像看起来像垃圾,因为显然色调旋转根本不是很准确。

A solution would be to have the CSS only target the text node of the span and not any child elements but that does not seem possible? 一个解决方案是让CSS只针对跨度的文本节点,而不是任何子元素,但这似乎不可能? Unless I'm missing something I don't see any selectors for text nodes. 除非我遗漏了一些内容,否则我看不到文本节点的任何选择器。

Is there something I can do in jQuery to perform this color inversion? 我可以在jQuery中执行这种颜色反转吗? I'd rather not have to destroy all the coloring on the page as that would upset the users. 我宁愿不必破坏页面上的所有颜色,因为这会让用户感到不安。

this Solution only goes lighter does not solve the problem: 这个解决方案只有更轻才解决问题:

I mis-read the question... I tried to delete this because it did not solve the problem for light backgrounds but it still appeared. 我错误地读了这个问题......我试图删除它,因为它没有解决浅背景的问题,但它仍然出现。

This can be accomplished by making use of rgba 这可以通过使用rgba来实现

Tested and works: 经过测试和工作:

$(window).load(function() {
      var col = $('.Comment').css('color').replace(')', ', 0.20)').replace('rgb', 'rgba');
      $('.Comment').css('color', col);    
});     

var col converts rgb to rgba allowing for color percentage var colrgb转换为rgba允许颜色百分比

Here's a solution that uses the javascript library tinycolor. 这是一个使用javascript库tinycolor的解决方案。 A pure CSS solution would be much preferred but it doesn't seem possible. 纯CSS解决方案将是首选,但似乎不可能。

$.getScript('https://rawgit.com/bgrins/TinyColor/master/tinycolor.js').done( function() {
    $('.Comment span[style^=color]').each(function() {
        var color = tinycolor($(this).css('color')).toHsl();
        color.l = 1 - color.l;
        $(this).css({'color': tinycolor(color).toRgbString()});
    });
});

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

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