按照中概述的步骤进行操作这个答案 https://stackoverflow.com/a/24093429/5090789,我将光标设置为 FontAwesome 图标。现在,我想按类名称将光标设置到任何图标(例如,fa-pencil
)。
为了实现这一点,我似乎需要能够以编程方式查找给定图标的 unicode 值。
我知道这些值列在font-awesome.css
样式表,但如果存在其他方法,我想避免解析该文件。
这可能吗?
可能晚了,但这可以让你这样做:elt.innerHTML = faUnicode('pencil');
也许它可以帮助其他人寻找同样的东西。
function faUnicode(name) {'use strict';
// Create a holding element (they tend to use <i>, so let's do that)
const testI = document.createElement('i');
// Create a realistic classname
// - maybe one day it will need both, so let's add them
testI.className = `fa fa-${name}`;
// We need to append it to the body for it to have
// its pseudo element created
document.body.appendChild(testI);
// Get the computed style
const char = window.getComputedStyle(
testI, ':before' // Add the ':before' to get the pseudo element
).content.replace(/'|"/g, ''); // content wraps things in quotes
// which we don't want
// Remove the test element
testI.remove();
return char.charCodeAt(0);
}
或者在 ECMA5 中:
function faUnicode(name) {
var testI = document.createElement('i');
var char;
testI.className = 'fa fa-' + name;
document.body.appendChild(testI);
char = window.getComputedStyle( testI, ':before' )
.content.replace(/'|"/g, '');
testI.remove();
return char.charCodeAt(0);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)