考虑以下JavaScript,CSS和HTML代码:
console.log(getComputedStyle(document.querySelector('p'), '::before').getPropertyValue('content'));
p::before {
content: "Apparently we \" \" had " attr(data-test) " \" a \" great \" height \"in\" the atmosphere";
}
<p data-test="reached"></p>
我正在尝试通过JavaScript获取CSS内容属性的值并将其插入DOM。
该值可能包含一个或多个带引号的引号,以便转义,例如上面的示例。
在Firefox上,返回值(包括attr(data-test))都易于解析和转换,因为保留了引号之前的反斜杠:
"Apparently we \" \" had " attr(data-test) " \" a \" great \" height \"in\" the atmosphere"
Chrome更进一步,在返回值之前转换attr(data-test),这也很好:
"Apparently we \" \" had reached \" a \" great \" height \"in\" the atmosphere"
但是在IE11上,反斜杠都在返回值中消失了,这似乎使得无法识别哪些引号用于包装字符串,哪些引号是字符串的一部分:
"Apparently we " " had " attr(data-test) " " a " great " height "in" the atmosphere"
是否可以使IE保留这些反斜杠,以便可以对返回的值进行解析和转换?