Javascript 如何使用css或js更改带有图标的文本

Javascript 如何使用css或js更改带有图标的文本,javascript,jquery,css,Javascript,Jquery,Css,我正在尝试使用星形图标更改我的表格(重力表格)中的数字(文本)“1”: 我想一定是这样的: <script> $("#label_13_5_0").text(function () { return $(this).attr("1").text().replace("1", "star"); }); </script> $(“#label_13_5_0”)。文本(函数(){ 返回$(this.attr(“1”).text()。替换(“1”,“星”); });

我正在尝试使用星形图标更改我的表格(重力表格)中的数字(文本)“1”:

我想一定是这样的:

<script>
$("#label_13_5_0").text(function () {
    return $(this).attr("1").text().replace("1", "star");
});
</script>

$(“#label_13_5_0”)。文本(函数(){
返回$(this.attr(“1”).text()。替换(“1”,“星”);
});
这行不通

我的网站(你可以在我的表格中看到要用星星替换的数字):


可能是这样,但我认为您需要安装fa图标

<script>
$("#label_13_5_0").text(function () {
    return $(this).text("<i class='far fa-star'></i>");
});
</script>

$(“#label_13_5_0”)。文本(函数(){
返回$(this).text(“”);
});
但以你为例:

<script>
$("#label_13_5_0").text(function () {
    return $(this).text("star");
});
</script>

$(“#label_13_5_0”)。文本(函数(){
返回$(此).text(“星”);
});

// --------------------------------------------------------
//
//1)通过标签进行循环
//2)获取文本并将其转换为数字
//3)创建一个字符串以保存替换html
//4)根据数字进行while循环并添加
//每一根绳子上都有一根绳子
//循环num——将从上的数字中减去一
//并使其停止在零位
//5)用图标标记替换innerHTML
//
// -------------------------------------------------------- 
$('label','.gfield_radio')。每个(函数(){
var num=编号(this.innerText);
var-str='';
而(num--)str+='';
this.innerHTML=str;
});
您得到的是“typeError$不是一个函数”-就好像您的页面中没有包含jQuery一样?而且,这个
替换(“1”,“星”)只是将字符串“1”替换为字符串“星形”。
<!-- include fontawesome in your html -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />



// --------------------------------------------------------
//
//  1) loop through the labels 
//  2) get the text and convert it to number
//  3) create a string to hold the replacement html
//  4) make a while loop based on the number and add 
//     <i class="fa fa-star"></i> to the string on each 
//     loop the num-- will subtract one from number on
//     each loop and cause it to stop at zero
//  5) replace the innerHTML with the icon markup
//
// -------------------------------------------------------- 
$('label', '.gfield_radio').each(function(){   
    var num = Number(this.innerText);               
    var str = '';
    while(num--) str+='<i class="fa fa-star"></i>';
    this.innerHTML = str;
});