简体   繁体   English

使用jQuery进行简单的dom遍历

[英]Simple dom traversal using jquery

I am working with a prior developers star rating code. 我正在与之前的开发人员星级评定代码一起使用。 I need to modify it because it doesn't operate the way the developer intended. 我需要对其进行修改,因为它无法按照开发人员的预期进行操作。 If you look, there are label elements with stars inside. 如果您看,里面有带有星label元素。 You can see the first label has 1 span , while the 5th label has 5 spans . 您可以看到第一个label span 1,而第5个label spans 5。

An example of how it's working now is you click on the 5th input the developer has 5 stars inside the label , but to get it to work as intended I actually need to modify the other label spans as well . 现在如何工作的一个示例是,单击开发人员在label有5个星label第5个input ,但是要使其按预期工作, 我实际上还需要修改其他标签跨度 So, let's say the user chooses 3 stars, I'd need to mutate the span inside labels 1, 2, and 3. For choosing 4 stars, I'd need to mutate the span inside labels 1, 2, 3, and 4. 因此,假设用户选择了3个星,我需要更改标签1、2和3内的span 。对于选择4个星,我需要更改标签1、2、3和4内的span

I was thinking about a jquery/javascript solution that traverses labels (maybe with ids). 我正在考虑遍历标签(可能带有ID)的jquery / javascript解决方案。 So if they click on input 4, the javascript solution would loop through all spans inside labels 1, 2, 3, and 4 and update the css. 因此,如果他们单击输入4,则javascript解决方案将遍历标签1、2、3和4内的所有范围,并更新CSS。 I'm not sure how to do this though. 我不确定如何执行此操作。

<div class="rating-selector">
    <label>
        <input type="radio" name="rating" value="1" />
        <span class="icon">★</span>
    </label>
    <label>
        <input type="radio" name="rating" value="2" />
        <span class="icon">★</span>
        <span class="icon">★</span>
    </label>
    <label>
        <input type="radio" name="rating" value="3" />
        <span class="icon">★</span>
        <span class="icon">★</span>
        <span class="icon">★</span>   
    </label>
    <label>
        <input type="radio" name="rating" value="4" />
        <span class="icon">★</span>
        <span class="icon">★</span>
        <span class="icon">★</span>
        <span class="icon">★</span>
    </label>
    <label>
        <input type="radio" name="rating" value="5" />
        <span class="icon">★</span>
        <span class="icon">★</span>
        <span class="icon">★</span>
        <span class="icon">★</span>
        <span class="icon">★</span>
    </label>
</div>

You could find the parent <label> index by using .closest("label").index() . 您可以使用.closest("label").index()找到父<label>索引。 Then, use the :lt() selector to grab each item before it. 然后,使用:lt()选择器抓取每个项目。

 const highlightPrevious = (elem) => { let $this = $(elem); $(".rating-selector label span").css("color", ""); //reset styling let idx = $this.index(); //get index of selected label $(`.rating-selector label:lt(${idx+1}) span`).css("color", "red"); //style all label spans up to and including the selected index }; $('label') .click(function() { highlightPrevious(this); }) .mouseover(function() { $(this).click() }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="rating-selector"> <label> <input type="radio" name="rating" value="1" /> <span class="icon">★</span> </label> <label> <input type="radio" name="rating" value="2" /> <span class="icon">★</span> <span class="icon">★</span> </label> <label> <input type="radio" name="rating" value="3" /> <span class="icon">★</span> <span class="icon">★</span> <span class="icon">★</span> </label> <label> <input type="radio" name="rating" value="4" /> <span class="icon">★</span> <span class="icon">★</span> <span class="icon">★</span> <span class="icon">★</span> </label> <label> <input type="radio" name="rating" value="5" /> <span class="icon">★</span> <span class="icon">★</span> <span class="icon">★</span> <span class="icon">★</span> <span class="icon">★</span> </label> </div> 

There is a simpler pure CSS solution: 有一个更简单的纯CSS解决方案:

 .rating-selector { display: inline-block; border: 0; direction: rtl; } input[name="rating"] { display: none; } input[name="rating"]~label { color: grey } input[name="rating"]:checked~label { color: gold } 
 <fieldset class="rating-selector"> <input type="radio" name="rating" value="5" id="rating-5" /> <label for="rating-5">★</label> <input type="radio" name="rating" value="4" id="rating-4" /> <label for="rating-4">★</label> <input type="radio" name="rating" value="3" id="rating-3" /> <label for="rating-3">★</label> <input type="radio" name="rating" value="2" id="rating-2" /> <label for="rating-2">★</label> <input type="radio" name="rating" value="1" id="rating-1" /> <label for="rating-1">★</label> </fieldset> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM