javascript - 添加评级星级 JavaScript 错误

标签 javascript

我正在尝试实现评级系统。

我尝试过:

When user clicks any of the stars, I try to add a class to stars before it and clicked star.

但它为 td 中的所有明星增添了档次

    <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <thead>
        <tr>
            <th>&nbsp;</th>
            <th>Rating </th>

        </tr>
        </thead>
        <tbody>

        <tr>
            <td class="hd">usertext1</td>
            <td>&nbsp;</td>

            <td>
                <i class="fa fa-star rating" aria-hidden="true"></i>
                <i class="fa fa-star rating" aria-hidden="true"></i>
                <i class="fa fa-star rating" aria-hidden="true"></i>
                <i class="fa fa-star rating" aria-hidden="true"></i>
                <i class="fa fa-star rating" aria-hidden="true"></i>

            </td>
        </tr>

        <tr>
            <td class="hd">usertext</td>
            <td>&nbsp;</td>

            <td>
                <i class="fa fa-star rating" aria-hidden="true"></i>
                <i class="fa fa-star rating" aria-hidden="true"></i>
                <i class="fa fa-star rating" aria-hidden="true"></i>
                <i class="fa fa-star rating" aria-hidden="true"></i>
                <i class="fa fa-star rating" aria-hidden="true"></i>

            </td>
        </tr>

        <tr>
            <td class="hd">feedback text</td>
            <td>&nbsp;</td>

            <td>
                <i class="fa fa-star rating" aria-hidden="true"></i>
                <i class="fa fa-star rating" aria-hidden="true"></i>
                <i class="fa fa-star rating" aria-hidden="true"></i>
                <i class="fa fa-star rating" aria-hidden="true"></i>
                <i class="fa fa-star rating" aria-hidden="true"></i>

            </td>
        </tr>

        </tbody>
    </table>


    <style>
    .ratingstar{
      color:#FFD700
    }
    </style>
    <script type="text/javascript">
    $('.rating').on('click', function(){
       var sel = $(this);
       var td = sel.parents('td').find('.rating');
       td.each(function(item){
           if($(this)==sel){
             console.log(item);//I tried break here but It throws error

           }
           $(this).addClass('ratingstar');
       })
    });
    </script>

最佳答案

使用链接实际上非常简单:

$('.rating').on('click', function(){
    $(this)                     // Our starting point, we're going left/right of this one
        .addClass('starred')    // Of course, activate the one selected
        .nextAll()              // Select the "unstarred"
        .removeClass('starred') // Deactivate greater ratings not selected
        .end()                  // This cancels the .nextAll() sub-selector, back to this
        .prevAll()              // Get the lower stars
        .addClass('starred')    // Activate them
    ;
});

https://jsfiddle.net/9364fusb/2

关于javascript - 添加评级星级 JavaScript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44472147/

相关文章:

javascript - 当函数不可用时重新定义函数,否则使用原始函数

javascript - 从远程 JSON 文件绘制 Highcharts 图表

javascript - JSX Prop 不应该使用 .bind() - 如何避免使用绑定(bind)?

javascript - 无法让标签与 D3-tile 投影一起使用

javascript - ExtJS ComboBox - 选择“所有项目”时在字段中显示 "Any"

javascript - 如何在对象上调用字符串中的函数?

javascript - 如何在文本框填充时隐藏 div 类

javascript - 如何通过javascript获取图像的大小?

javascript - 使用 Firebase 的服务器端计算

javascript - 获取网页字段内容到Javascript变量 [Firefox Addon-SDK]