这个数组有一个小问题。
例如,我需要在每个值为 0 的键中添加 css 类,这是我的文章:
var obj = {"test_1":"1","test_2":"1","test_3":"0","test_4":"0","test_5":"0","test_6":"1","test_7":"0"};
$.each( obj, function( key, value ) {
alert("key " + key + " has value " + obj[key]);
if(value = 0) {
$('#'+ key).addClass('hidden');// hidden only the obj has value = 0
}
});
最佳答案
您正在将 0
分配给变量 value
,它将始终返回 true。将其替换为 if(value==0)
,这样它将返回一个 bool 值。
关于javascript - jQuery 数组 : Add css class to specific element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34527435/