迭代所有元素和基于innerHtml的addClass [英] Iterate through all elements and addClass based on innerHtml

查看:106
本文介绍了迭代所有元素和基于innerHtml的addClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我已经完成了这一半工作:

我想要选择所有带有'status-holder'类的元素。然后检查该元素的innerHtml是什么,然后根据值添加一个CSS类,现有代码:



 $(< span class =code-string>'  .status-holder')。each(function(){
switch ($( this )。html()。trim()){
案例 已发送
alert( fodo);
$( this ).addClass(' status-sent');
案例 收到
$( this )。addClass(' status-received');
case 正在进行中
$( this )。addClass(' 状态-INPROGRESS' );
case 已完成
$( this )。addClass(' 状态完成的);
};
});



问题是这不仅仅改变当前的迭代(我想要的)它改变了所有元素的'status-holder类。我认为$(this)将是当前的迭代,而不是指向选择器。



任何帮助都会很棒。

解决方案

' .status-holder')。each(function(){
switch


this )。html ()。trim()){
case 已发送
alert( fodo);


this )。addClass(' status-sent');
case 收到

ok so i've got this half working:
I'm wanting to select all elements with a class of 'status-holder'. Then check what the innerHtml is of that element, then based on the value add a CSS class, existing code:

$('.status-holder').each(function () {
            switch ($(this).html().trim()) {
                case "Sent":
                    alert("fodo");
                    $(this).addClass('status-sent');
                case "Received":
                    $(this).addClass('status-received');
                case "In Progress":
                    $(this).addClass('status-inProgress');
                case "Completed":
                    $(this).addClass('status-completed');
            };
        });


problem is this doesn't just change the current iteration (what i want) it changes all the elements with class of 'status-holder'. i thought $(this) would be the current iteration, not refer to the selector.

Any help would be great.

解决方案

('.status-holder').each(function () { switch (


(this).html().trim()) { case "Sent": alert("fodo");


(this).addClass('status-sent'); case "Received":


这篇关于迭代所有元素和基于innerHtml的addClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆