相关疑难解决方法(0)

我可以通过JavaScript(getComputedStyle)在{content:'...'}字符串之前阅读:但这个字符串表现得很奇怪

我有以下设置.我创建了一个<div>,通过伪选择器附加术语"之前",:before并通过使用读取该值getComputedStyle.

这是有效的,我得到的术语(在我的情况下是"之前")成功,它是"字符串"类型.(参见控制台输出.)

该字符串与给定字符串的比较将返回预期的true,但仅在Safari,CodePen和此处的"运行代码片段" - 环境中!

它不适用于Chrome,Firefox或IE.比赛的比较返回false.

可能是什么原因呢?

为什么这个简单的字符串比较不起作用?

代码的三个相关代码段如下所示:

var content = window.getComputedStyle(document.querySelector('.pseudo'), '::before').getPropertyValue('content');
console.log('content: ' + content); // "before"
console.log('typeof content: ' + typeof content); // string
console.log(content == "before"); // false
document.write(content == "before"); // false
Run Code Online (Sandbox Code Playgroud)
div.pseudo:before {
  content: "before";
  color: red;
}
Run Code Online (Sandbox Code Playgroud)
<div class="pseudo">
  Div with pseudo :before content.
</div>
Run Code Online (Sandbox Code Playgroud)

javascript css

6
推荐指数
1
解决办法
1244
查看次数

标签 统计

css ×1

javascript ×1