【问题标题】:How to set icon content of pseudo element by its attribute? [duplicate]如何通过属性设置伪元素的图标内容? [复制]
【发布时间】:2020-08-06 09:58:26
【问题描述】:

我正在使用 icomoon 作为我的图标库

我正在尝试通过属性data-icon 动态设置图标。

但使用attr(data-icon) 似乎无法将内容识别为字符集。

有没有办法使用attr()\e92c 设为字符而不是字符串文字?

#expected::before {
  content: '\e92c';
  font-family: 'icomoon';
}

[data-icon]::before {
  content: attr(data-icon);
  font-family: 'icomoon';
}
<h2>This is what I expected</h2>
<a class="button" id="expected" href="javascript: void(0)">Save</a>

<h2>This is what I get :(</h2>
<a class="button" data-icon="\e92c" href="javascript: void(0)">Save</a>

【问题讨论】:

    标签: html css icomoon


    【解决方案1】:

    HTML 文本与 CSS 文本的转义方式不同。在属性中,您希望 &amp;#xE92C; 用于等效的十六进制 excape(规范称为“十进制字符引用”或“十六进制字符引用;”details here)。

    十六进制字符引用(注意# 后面的x):

    data-icon="&#xE92C;"
    

    十进制(无x):

    data-icon="&#59692;"
    

    活生生的例子:

    #expected::before {
      content: '\00e92c';
      font-family: 'icomoon';
    }
    
    [data-icon]::before {
      content: attr(data-icon);
      font-family: 'icomoon';
    }
    <h2>This is what I expected</h2>
    <a class="button" id="expected" href="javascript: void(0)">Save</a>
    
    <h2>From <code>data-icon</code></h2>
    <a class="button" data-icon="&#xE92C;" href="javascript: void(0)">Save</a>

    【讨论】:

    • 哇,我不知道!效果很好,谢谢!将在 2 分钟内接受!
    猜你喜欢
    • 2015-05-06
    • 2013-09-26
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 2012-09-25
    • 1970-01-01
    • 2016-01-01
    相关资源
    最近更新 更多