jquery,如何使用 jquery 添加 HTML 十六进制图标类?

标签 jquery html css

我正在尝试使用 jquery 添加样式,该样式包含 ascii 图标 CSS 类:

.plusIcon:before {
content: "\2b";}
.minusIcon:before {
content: "\2212";}

当我从样式表加载样式时,它工作正常: 加号图标内容为“\2b”(html页面显示“+”),减号图标内容为“\2212”(html页面显示“-”)

enter image description here

enter image description here

,但是当我用 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”)

enter image description here

enter image description here

有没有什么方法可以用 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/

相关文章:

html - 无法将图片和文字彼此对齐

带有过渡的 CSS

javascript - Selectize.js getAdjacentOption() 如何工作?

javascript - fullPage.js 和固定导航 - 想要在导航中突出显示事件页面

javascript - 输入类型 ="date"占位符 - 移动设备

javascript - 如何包装单词看起来像标签?

javascript - 将元素移动到另一个位置

jquery - 使用 jQuery 更改响应式网站中的 HTML 结构

html - 正确对齐 CSS 菜单

javascript - 如何从 html 类中删除额外的 ""组?