首页 > 解决方案 > 之前的jquery伪元素,内部带有反斜杠的内容来自attr(data-before)

问题描述

我有一个小问题。我想使用 jQuery 更改字形图标(图标字体),如下所示:

var icoList = 'E251'

var icon = $('ul > li:has(ul)', $this);
icon.addClass('icon');

icon.attr('data-before', '&#x' + icoList);          // this doesn' work, -> &#xE251
icon.attr('data-before', '\' + icoList);            // this doesn' work, -> TypeError
icon.attr('data-before', '\\' + icoList);           // this doesn' work, -> \E251
icon.attr('data-before', '"\\' + icoList + '"');    // this doesn' work, -> "\E251"

我只得到一个字符串。

我的CSS是这样的:

ul li::before {
    font-family: "Glyphicons Halflings";
    content: attr(data-before);
}

在我的 HTML 中,我尝试了这个:

<nav id="Nav-Main" class="nav-main">
    <ul>
        <li data-before="&#xE251">              <!--  this works fine  -->
            <a href="#">Home</a>

            <ul class="flexy">
                <li>
                    <a href="#">Referenzen</a>
                </li>
                <li>
                    <a href="#">Downloads</a>
                </li>
            </ul>
        </li>
    </ul>
</nav>

如果我在我的 html 代码中使用数据,那么它工作正常。

我做错了什么?如何使用代码获取此图标?

标签: jquerycssicon-fonts

解决方案


推荐阅读