我正在尝试使用 jquery 添加样式,该样式包含 ascii 图标 CSS 类:
.plusIcon:before {
content: "\2b";}
.minusIcon:before {
content: "\2212";}
当我从样式表加载样式时,它工作正常: 加号图标内容为“\2b”(html页面显示“+”),减号图标内容为“\2212”(html页面显示“-”)
,但是当我用 jquery 加载 css 样式时,内容发生了变化
j查询代码:
var css = '.minusIcon:before {content: "\2212";}.plusIcon:before {content: "\2b";}'
jQuery('head').append('<style>' + css + '</style>')
标签添加到我的 html 页面但类内容更改为: 加号图标内容为“2b”(在html页面上显示“2b”),减号图标内容为“2”(在html页面上显示“2”)
有没有什么方法可以用 jquery 添加这种样式以显示这些图标?
提前致谢
最佳答案
请查看以下更改。每当你通过 JS 字符串处理它们时,你需要用双反斜杠转义单反斜杠:
var css = '.minusIcon:before {content: "\\2212";}.plusIcon:before {content: "\\2b";}'
jQuery('head').append('<style>' + css + '</style>')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<span class="minusIcon"></span>
</br>
<span class="plusIcon"></span>
关于jquery,如何使用 jquery 添加 HTML 十六进制图标类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38265496/