因此,我试图制作一个星级功能,但我一直坚持从变量中动态设置值.我不想让明星们互动.我希望使用变量的值来设置或填充它们.

这就是我得到星星的方式

<div>
    <span></span>
    <span class="rating" data-rating="0">
        <i class="star" data-checked="false" data-note="1">&#9733;</i>
        <i class="star" data-checked="false" data-note="2">&#9733;</i>
        <i class="star" data-checked="false" data-note="3">&#9733;</i>
        <i class="star" data-checked="false" data-note="4">&#9733;</i>
        <i class="star" data-checked="false" data-note="5">&#9733;</i>
    </span>
    <span>
        <input class="coach-id" hidden value="{{ coach.id }}">
    </span>
</div>

现在,我的问题是,我要为数据库中的条目生成n次这段代码.这就产生了一个问题,即每个块都将具有相同的类.

我希望能够通过特定于每个块的变量动态设置它们.这意味着数据库中的每个条目都有一个评级列,该列将是对应于该条目的星星值.

现在,明星们通过下面的脚本进行互动

const ratings = document.querySelectorAll('.rating');
const coach_id = document.querySelector('.coach-id').value;
ratings.forEach(rating =>
    rating.addEventListener('mouseleave', ratingHandler)
);
const stars = document.querySelectorAll('.rating .star');
stars.forEach(star => {
    star.addEventListener('mouseover', starSelection);
    star.addEventListener('mouseleave', starSelection);
    star.addEventListener('click', activeSelect);
});
function ratingHandler(e) {
    const childStars = e.target.children;
    for(let i = 0; i < childStars.length; i++) {
        const star = childStars.item(i)
        if (star.dataset.checked === "true") {
            star.classList.add('hover');
        }
        else {
            star.classList.remove('hover');
        }
    }
}

function starSelection(e) {
    const parent = e.target.parentElement
    const childStars = parent.children;
    const dataset = e.target.dataset;
    const note = +dataset.note; // Convert note (string) to note (number)
    for (let i = 0; i < childStars.length; i++) {
        const star = childStars.item(i)
        if (+star.dataset.note > note) {
            star.classList.remove('hover');
        } else {
            star.classList.add('hover');
        }
    }
}
function activeSelect(e) {
    const parent = e.target.parentElement
    const childStars = parent.children;
    const dataset = e.target.dataset;
    const note = +dataset.note; // Convert note (string) to note (number)
    for (let i = 0; i < childStars.length; i++) {
        const star = childStars.item(i)
        if (+star.dataset.note > note) {
            star.classList.remove('hover');
            star.dataset.checked = "false";
        } else {
            star.classList.add('hover');
            star.dataset.checked = "true";
        }
    }
    const noteTextElement = parent.parentElement.lastElementChild.children.item(0)
    noteTextElement.innerText = `Note: ${note}`;
    console.log(note);
    let url = `/coach/editRating/${coach_id}/${note}`
    window.location.href = url
    console.log(url)
}

推荐答案

您好@Moudhaffer Bouallegui-请查看星级评定页面示例.

document.querySelectorAll("i.fa-star").forEach(function(element,position){
            element.addEventListener("click",function(){
                this.style.color = "yellow";
              //  this.style.fontSize = "35px";
                let noOfStar = this.parentNode.parentNode.querySelector("input[name=star]").value;

                document.querySelectorAll("i.fa-star").forEach(function(e,p){
                    e.style.color = "rgb(126, 126, 126)";
                    
                    if((p+1) <= noOfStar){
                        e.style.color = "yellow";
                    }
                })

            })

           
        })
    input[type=radio]{
        display: none;
    }
    .card{
        max-width: 350px;
        text-align: center;
        margin: auto;
        box-shadow: 0 0 15px 0 #000;
        padding: 15px;
        background-color: #000;
        color: #fff;
        cursor : pointer;
    }
    .fa-star{
        font-size: 30px;
        color: rgb(126, 126, 126);
        transition: 0.1s ease-in-out all;
    }
    .stars{
        padding:10px;
        height: 40px;
        
    }
    button{
        width: 70%;
        outline: none;
        border: none;
        background-color: rgb(107, 107, 107);
        padding:10px;
        color: #fff;
        border-radius: 0;
    }
    button:hover{
        box-shadow: 0 0 30px 0 yellow;
        border: 1px solid #fff;
    }
    .fa-star{
        cursor: pointer;
    }
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
    <div class="card">
        <h2>Give your stars</h2>
        <div class="stars">
            <span><label><i for="star1" class="fa fa-star"></i></label><input type="radio" name="star" id="star1"
                    value="1"></span>
            <span><label><i for="star2" class="fa fa-star"></i></label><input type="radio" name="star" id="star2"
                    value="2"></span>
            <span><label><i for="star3" class="fa fa-star"></i></label><input type="radio" name="star" id="star3"
                    value="3"></span>
            <span><label><i for="star4" class="fa fa-star"></i></label><input type="radio" name="star" id="star4"
                    value="4"></span>
            <span><label><i for="star5" class="fa fa-star"></i></label><input type="radio" name="star" id="star5"
                    value="5"></span>
        </div>
       
    </div> 

Javascript相关问答推荐

按钮未放置在html dis位置

通过在页面上滚动来移动滚动条

在React中获取数据后,如何避免不必要的组件闪现1秒?

按下同意按钮与 puppeteer 师

如何从一个列表中创建一个2列的表?

使用LocaleCompare()进行排序时,首先使用大写字母

无法从NextJS组件传递函数作为参数'

更改预请求脚本中重用的JSON主体变量- Postman

如何在箭头函数中引入or子句?

在FAQ Accodion应用程序中使用React useState时出现问题

try 使用PM2在AWS ubuntu服务器上运行 node 进程时出错

当标题被点击时,如何使内容出现在另一个div上?

如何在Press上重新启动EXPO-AV视频?

删除加载页面时不存在的元素(JavaScript)

警告框不显示包含HTML输入字段的总和

如何从Reaction-Redux中来自API调用的数据中筛选值

如何从图表中映射一组图表-js使用REACT

是否在图表中计算线上的点坐标?

Oracle APEX-如何调用Java脚本函数

当触发AJAX POST请求时,绕过对PHP生成的表的加载函数