简体   繁体   English

为什么document.execCommand(''backColor'')不能与css变量一起使用,而foreColor却可以呢?

[英]Why document.execCommand(''backColor'') does not work with css variable while foreColor does?

I need to execute document.execCommand('backColor') command with css variable , but something goes wrong. 我需要使用CSS变量执行document.execCommand('backColor')命令,但是出了点问题。 No background style added at all. 完全没有添加背景样式。 foreColor works as expected and adds color style with the variable. foreColor可以正常工作,并使用该变量添加颜色样式。 I am doing this within Angular 7 environment. 我正在Angular 7环境中执行此操作。

 function redBg() { document.execCommand('styleWithCSS', true, null); document.execCommand('backColor', false, 'var(--red)'); } function redFg() { document.execCommand('styleWithCSS', true, null); document.execCommand('foreColor', false, 'var(--red)'); } 
 .content { background: #ccc; width: 100%; } .button { font-size: 20px; background: #f00; user-select: none; margin: 5px; } --red: red; 
 <div class="content" contentEditable="true"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </div> <div class="button" onmousedown="redBg()">Red bg</div> <div class="button" onmousedown="redFg()">Red fg</div> 

The value argument must be a CSS <color> per: value参数必须是每个CSS <color>

This requires a value string to be passed in as a value argument. 这需要将值字符串作为值参数传入。

and specified per one of these methods: 并按以下任一方法指定:

https://developer.mozilla.org/en-US/docs/Web/CSS/color_value https://developer.mozilla.org/zh-CN/docs/Web/CSS/color_value

Using a custom CSS property isn't a CSS <color> 使用自定义CSS属性不是CSS <color>

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

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