如何使用Javascript从伪元素获取FontAwesome字符代码

Điề*_*uan 2 javascript css font-awesome

我们可以获得伪元素:

elem   = document.getElementById('element');
result = window.getComputedStyle(elem, ':before').getPropertyValue('content');
Run Code Online (Sandbox Code Playgroud)

但结果返回“?”

有没有办法从 CSS 文件中获取值?(类似于“\a8xx”)


感谢大家的回答,我找到了解决我的问题的方法:

icons = $('body *');

for (var i = 0; i < icons.length; i++)
{
    elem = icons[i];
    before_content = window.getComputedStyle(elem, ':before').getPropertyValue('content').charCodeAt(1).toString(16);
    after_content  = window.getComputedStyle(elem, ':after' ).getPropertyValue('content').charCodeAt(1).toString(16);

    console.log(before_content, after_content);
}
Run Code Online (Sandbox Code Playgroud)

zer*_*kms 5

就像

'?'.charCodeAt(0) // 57661
Run Code Online (Sandbox Code Playgroud)

如果您想将其转换为十六进制,请应用toString(16)

'?'.charCodeAt(0).toString(16) // e13d
Run Code Online (Sandbox Code Playgroud)

参考:

  • 可能值得添加 `// 57661` 作为代码的注释(甚至作为注释);只是为了清楚起见并避免人们(像我一样!)跑到他们的控制台查看;您的代码也明确假设它是一个单字符字符串。 (2认同)
  • @ĐiềnBáQuan 代码点 `34` 是一个引号 (`"`) 字符。检查你运行它的字符串 (2认同)