简体   繁体   English

遍历无序列表以添加和删除类

[英]Looping through unordered list to add and remove classes

I am trying to iterate through an unordered list and loop through each list item (with the class of ticker-item ) and temporarily append a class to it so that CSS can apply some transitions to it). 我正在尝试遍历无序列表,并遍历每个列表项(带有ticker-item类),并临时向其添加一个类,以便CSS可以对其进行一些转换。

var a = [];
$(".ticker-item").each(function(index) {
  a[index] = $(this);
  setTimeout(function() {
    a[index].addClass('current').delay(500).queue(function(next) {
      a[index].removeClass('current');
      next();
    });
  }, index * 500);
});

I've searched through various SO articles, and I'm confident that the function will add and remove the classes correctly, but it doesn't currently seem to be applying the li's? 我搜索了各种SO文章,并且我相信该函数将正确添加和删除类,但是当前似乎不使用li的吗?

And here is the HTML: 这是HTML:

      <div class="ticker">
        <ul class="ticker-items">
            <li class="ticker-item">
                <a href="/categories/featured/summer-sale/1185">
                    <p><strong>Summer Sale</strong>🍦 Now more things at <strong>50% off!</strong></p>
                </a>
            </li>
            <li class="ticker-item">
                <a href="/about">
                    <p>Things for kids you'll 💗 too. <strong>Our story</strong></p>
                </a>
            </li>
            <li class="ticker-item">
                <a href="/help/delivery-and-returns">
                    <p><strong>Free</strong> Delivery* &amp; <strong>Free</strong> Returns 📦</p>
                </a>
            </li>
        </ul>
    </div>

You do not loop through elements with the class ticker-item , you loop through elements that are like this: <ticker-item> ... </ticker-item> 您不使用类ticker-item遍历元素,而是遍历如下元素: <ticker-item> ... </ticker-item>

To change your loop you have to use the class-selecter: 要更改循环,您必须使用类选择器:

$(".ticker-item").each

you are not accessing by class name, 您没有按班级名称访问,

change $("ticker-item").each to $(".ticker-item").each (class selector) 将$(“ ticker-item”)。each更改为$(“。ticker-item”)。each(类选择器)

The addClass nor removeClass are using the queue by definition. 根据定义, addClassremoveClass都在使用队列。 So jQuery#delay will have no effect on these methods. 因此jQuery#delay将对这些方法无效。

The solution is to execute both of them into queued callbacks in order for them to be pushed into the queue, like the following: 解决方案是将它们都执行到排队的回调中,以便将它们推入队列,如下所示:

 setTimeout(() => { $('#noqueue') .addClass('colored') .delay(2000) .removeClass('colored'); $('#queue') .queue(function(next) { $(this).addClass('colored'); next(); }) .delay(2000) .queue(function() { $(this).removeClass('colored'); }); }, 1000); 
 div { transition: color .5s; } .colored { color: red; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="noqueue">no queue</div> <div id="queue">with queue</div> 

And I made a example below to chaining your animation. 我在下面举了一个链接动画的示例。

Note that I didn't use setTimeout but I only used jQuery#delay , as I find that's cleaner. 请注意,我没有使用setTimeout而是只使用jQuery#delay ,因为我发现它更干净。

Also, don't forget to dequeue your animations if you want to reexecute them without refreshing your page. 另外,如果要在不刷新页面的情况下重新执行动画,请不要忘记使动画出队。

 $('button').on('click', () => { $('ul > li').each((i, li) => { $(li) .delay(500 * (i + 1)) .queue(next => { $(li).addClass('colored'); next(); }) .delay(750) .queue(() => ($(li).removeClass('colored'), $(li).dequeue())); }); }); 
 ul > li { transition: color 1s; } .colored { color: red; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul> <li>Item</li> <li>Item</li> <li>Item</li> <li>Item</li> <li>Item</li> <li>Item</li> <li>Item</li> <li>Item</li> <li>Item</li> <li>Item</li> </ul> <button>Color them!</button> 

删除并重新添加 Class 属性<div>无序列表项</div><div id="text_translate"><p>这是我能够解决的答案。 想要在按钮单击事件从 Hyperlink1 发生到 Hyperlink3 后更改 css class="jsTree-clicked"。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $(document).ready(function () { //remove Class $('#myJSTree').find('.jsTree-clicked').removeClass('jsTree-clicked'); //need to do add it to List Item which has Id =3 //check the list item which has id =3 if so add the class to it // It is not a button click event. $('#myJSTree li').each(function (i, li) { console.log('&lt;li&gt; id =' + $(li).attr('id')); var myIdVal = $(li).attr('id'); if (myIdVal == 3) { $(this).addClass('jsTree-clicked'); } }); });</pre><pre class="snippet-code-css lang-css prettyprint-override"> .jsTree-clicked { background-color:red;}</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;div id="myJSTree"&gt; &lt;ul class="nav nav-list"&gt; &lt;li id="1"&gt; &lt;a class="jsTree-clicked" title="Hyperlink1"&gt;HyperLink1&lt;/a&gt; &lt;/li&gt; &lt;li id="2"&gt; &lt;a title="Hyperlink2"&gt;HyperLink2&lt;/a&gt; &lt;/li&gt; &lt;li id="3"&gt; &lt;a title="Hyperlink3"&gt;HyperLink3&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt;</pre></div></div><p></p><p> 单击超链接时,JsTree 添加一个 class="jsTree-clicked"。 当您导航到不同的节点时,它将删除并重新添加相同的 class 到导航节点。</p><p> 预期的</p><p>我想要一个 function 根据 div 内的 ID 删除给定列表项的 [class="jsTree-clicked"]。 并通过传递 Key ie ID 将 [class="jsTree-clicked"] 重新添加到任何 ListItem。</p><p> 我希望我能够解释我的问题。 谢谢</p><p>我的 JSTree 是第三方开源的。</p></div> - Remove and re-add Class Attribute inside a <div> unordered list item

暂无
暂无

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

相关问题 遍历数组时,如何从符合特定条件的元素中添加/删除类? - When looping through an array how do I add/remove classes from elements that fit a specific criteria? 如何删除超链接并在javascript创建的无序列表中添加类? - How to remove a hyperlink and add a class in a javascript created unordered list? 通过复选框添加和删除CSS类 - Add and remove css classes through checkboxes jQuery通过无序列表进行动画处理 - jQuery animate through unordered list 通过javascript创建无序列表 - Creating an unordered list through javascript 遍历课程并更改颜色 - Looping Through Classes And Changing Color 通过按钮列表添加和删除类 - Add and Remove a Class through a list of Buttons 如何在反应中迭代 DOM 元素并添加/删除类? - How to iterate through DOM elements in react and add/remove classes? 删除并重新添加 Class 属性<div>无序列表项</div><div id="text_translate"><p>这是我能够解决的答案。 想要在按钮单击事件从 Hyperlink1 发生到 Hyperlink3 后更改 css class="jsTree-clicked"。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $(document).ready(function () { //remove Class $('#myJSTree').find('.jsTree-clicked').removeClass('jsTree-clicked'); //need to do add it to List Item which has Id =3 //check the list item which has id =3 if so add the class to it // It is not a button click event. $('#myJSTree li').each(function (i, li) { console.log('&lt;li&gt; id =' + $(li).attr('id')); var myIdVal = $(li).attr('id'); if (myIdVal == 3) { $(this).addClass('jsTree-clicked'); } }); });</pre><pre class="snippet-code-css lang-css prettyprint-override"> .jsTree-clicked { background-color:red;}</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;div id="myJSTree"&gt; &lt;ul class="nav nav-list"&gt; &lt;li id="1"&gt; &lt;a class="jsTree-clicked" title="Hyperlink1"&gt;HyperLink1&lt;/a&gt; &lt;/li&gt; &lt;li id="2"&gt; &lt;a title="Hyperlink2"&gt;HyperLink2&lt;/a&gt; &lt;/li&gt; &lt;li id="3"&gt; &lt;a title="Hyperlink3"&gt;HyperLink3&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt;</pre></div></div><p></p><p> 单击超链接时,JsTree 添加一个 class="jsTree-clicked"。 当您导航到不同的节点时,它将删除并重新添加相同的 class 到导航节点。</p><p> 预期的</p><p>我想要一个 function 根据 div 内的 ID 删除给定列表项的 [class="jsTree-clicked"]。 并通过传递 Key ie ID 将 [class="jsTree-clicked"] 重新添加到任何 ListItem。</p><p> 我希望我能够解释我的问题。 谢谢</p><p>我的 JSTree 是第三方开源的。</p></div> - Remove and re-add Class Attribute inside a <div> unordered list item 用来选择div和添加/删除类的香草Javascript循环功能的异常行为 - Unexpected behaviour of vanilla Javascript looping function to select divs and add/remove classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM