首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么document.execCommand(‘backColor’)不适用于css变量,而foreColor却这样做?

为什么document.execCommand(‘backColor’)不适用于css变量,而foreColor却这样做?
EN

Stack Overflow用户
提问于 2018-11-19 22:20:15
回答 1查看 1.3K关注 0票数 0

我需要使用css变量执行document.execCommand('backColor')命令,但有些地方出错了。根本没有添加背景样式。foreColor按预期工作,并在变量中添加颜色样式。我是在角7环境下这样做的。

代码语言:javascript
复制
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)');
}
代码语言:javascript
复制
.content {
  background: #ccc;
  width: 100%;
}

.button {
  font-size: 20px;
  background: #f00;
  user-select: none;
  margin: 5px;
}

--red: red;
代码语言:javascript
复制
<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>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-19 22:47:52

值参数必须是CSS <color> (按:

这需要将值字符串作为值参数传入。

并按下列方法中的一种具体规定:

value

使用自定义CSS属性不是CSS <color>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53383478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档