使用 JQuery 更改子元素类
我试图更改 id 为“led1”的所有 div 元素的类,该元素驻留在另一个 id 为 1 的 div 中。
<div class="MACHINE">
<div id="1" class="HOST">EMPTY
<div id="led1" class="LED"></div>
</div>
</div>
我使用以下代码,但它只更改文本“EMPTY”,而不更改类。下面是我的一段 js 文件,用于更改类。由于某种原因它不起作用......
我希望有人能引导我走向正确的方向。
$.each(data, function(hst) {
$.each(data[hst], function(key, value){
var currClass = $('#led'+ key).attr('class');
switch(value)
{
case 'EMPTY': $('#led' + key).removeClass(currClass).addClass('LED ' + value);$('#' + key).html(value);break;
case 'PROCESSING': $('#led' + key).removeClass(currClass).addClass('LED ' + value);$('#' + key).html(value);break;
case 'FINISHED': $('#led' + key).removeClass(currClass).addClass('LED ' + value);$('#' + key).html(value);break;
case 'REPLACE': $('#led' + key).removeClass(currClass).addClass('LED ' + value);$('#' + key).html(value);break;
}
});
});
},
error: function() {
alert('Ello');
console.log("ERROR: show_host_status")
},
});
},
}
I am trying to change the class of all div element with the id "led1" which resides in another div with the id 1.
<div class="MACHINE">
<div id="1" class="HOST">EMPTY
<div id="led1" class="LED"></div>
</div>
</div>
I use the following code but it only change the text "EMPTY" and not the class. Below is a piece of my js file is use to change the class. For some reason it doesn't work...
I hope that someone can direct my into the right direction.
$.each(data, function(hst) {
$.each(data[hst], function(key, value){
var currClass = $('#led'+ key).attr('class');
switch(value)
{
case 'EMPTY': $('#led' + key).removeClass(currClass).addClass('LED ' + value);$('#' + key).html(value);break;
case 'PROCESSING': $('#led' + key).removeClass(currClass).addClass('LED ' + value);$('#' + key).html(value);break;
case 'FINISHED': $('#led' + key).removeClass(currClass).addClass('LED ' + value);$('#' + key).html(value);break;
case 'REPLACE': $('#led' + key).removeClass(currClass).addClass('LED ' + value);$('#' + key).html(value);break;
}
});
});
},
error: function() {
alert('Ello');
console.log("ERROR: show_host_status")
},
});
},
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到一个问题。
它将“HOST”的 HTML 设置为任何值(从而删除 LED)。尝试这样的事情。
以及您在 case 语句中的代码,
然后您必须更改获取“值”的方式,因为它不仅仅是来自 #key .host-value 的直接元素
I see a problem.
It sets the "HOST"'s HTML to whatever value is (thus removing the LED). Try something like this.
and your code in the case statements as
Then you have to change how your getting the "value" since its not just straight element its from #key .host-value
正如其他回复中所指出的,从技术上讲,答案是:
ID 和 NAME 令牌必须以字母 ([A-Za-z]) 开头,后面可以跟任意数量的字母、数字 ([0-9] )、连字符(“-”)、下划线(“_”)、冒号(“:”)和句点(“.”)
。致歉:@dgvid
尝试将
更改为
并相应地更新您的脚本
As pointed out in other responses, the answer is technically:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")
.apologies to: @dgvid
Try changing
<div id="1">
to<div id="host-1">
and update your script accordingly