javascript - 如何在 jquery 评级星中使用字体 Awesome 而不是图像

标签 javascript jquery css raty

我正在使用 jquery.raty.min.js 来显示星级,在 js 方法中我可以选择更改显示开始图像,如

starOff:"/images/star_none.png",

starOn:"/images/star_full.png"

但在这里我想使用像

这样的字体 Awesome 类
<i class="fa fa-star-o"></i>
<i class="fa fa-star"></i>

我试过像下面这样放置类

starOff:"<i class='fa fa-star-o'></i>",

starOn:"<i class='fa fa-star'></i>"

但它不起作用,任何人都可以帮忙做这件事。

最佳答案

对这个问题的简短回答是使用最近更新的 Raty (2.7.0) (截至此回复的最后一次编辑)并查看“starType”属性。当您设置元素时,您将执行如下操作:

$('div').raty({
  // This is the config you need to change the tag from image to icon
  starType : 'i'
});

这里有更多详细信息... 如果您查看 CSS文件和 fonts文件,您将看到他们如何设置 <i>标记以使用特定字体。从那里识别每个类的 :before 是一个简单的任务。内容会。首先确保包含 FontAwesome,然后像这样配置你的 CSS(特定于 FontAwesome):

.cancel-on-png, .cancel-off-png, .star-on-png, .star-off-png, .star-half-png {
  -moz-osx-font-smoothing: grayscale;
  -webkit-font-smoothing: antialiased;

  /*This is the important part*/
  font-family: "FontAwesome";

  font-style: normal;
  font-variant: normal;
  font-weight: normal;
  line-height: 1;
  speak: none;
  text-transform: none;
}

.cancel-on-png:before {
  /*the "\f057" represents an unfilled circle with an x in it*/
  /*each of these can be configured to be whichever icon you would like*/
  /*fore more information on the icon code, look at the link listed below*/
  content: "\f057";
}

.cancel-off-png:before {
  content: "\f05c";
}

.star-on-png:before {
  content: "\f005";
}

.star-off-png:before {
  content: "\f006";
}

.star-half-png:before {
  content: "\f123";
}

可以找到 FontAwesome 的 CSS 内容值的完整列表 here


最终产品看起来像这样:

<div>
  <i title="Cancel this rating!" class="raty-cancel cancel-off-png" data-alt="x"></i>&nbsp;
  <i data-alt="1" class="star-off-png" title="Bad"></i>&nbsp;
  <i data-alt="2" class="star-off-png" title="Below Average"></i>&nbsp;
  <i data-alt="3" class="star-off-png" title="Average"></i>&nbsp;
  <i data-alt="4" class="star-off-png" title="Above Average"></i>&nbsp;
  <i data-alt="5" class="star-off-png" title="Great"></i>
  <input name="score" type="hidden">
</div>

当这样配置时:

$(div).raty({
  readOnly    : readOnly,
  cancel      : !readOnly,
  noRatedMsg  : 'This component has not been rated yet',
  half        : true,
  starType    : 'i',
  hints       : ['Bad', 'Below Average', 'Average', 'Above Average', 'Great'],
  click       : function(score, event) {
    console.log("The score is:", score);
  }
});

我知道已经过去 3 个月了,但我希望这对某人有所帮助!

关于javascript - 如何在 jquery 评级星中使用字体 Awesome 而不是图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22039546/

相关文章:

javascript - 无序发生的属性更改的 Angular 事件

javascript - 查找字符,除非被特定字符包围

java - 获取通过 Java Servlet 中的 jquery ajax 发送的参数

Javascript 问题,需要根据 2 <select> 中的选择对表内的值进行着色

javascript - 我的 jQuery 代码不适用于 WebStorm,但它适用于 CodePen

javascript - 仅为数字字符指定字体?

javascript - 在 Javascript 中更改光标类型

javascript - 侧边栏在顶部打开?

javascript - 如何获取使用 data-dojo-attach-point 设置的 Dojo 节点的对象描述符?

javascript - 使用 ng-view 并按需加载 Controller