I am passing back a JSON object to jQuery. I need it to look th开发者_JAVA技巧rough the JSON object and effectively do this.
$(key).css('background-color', '#'+val);
In php I would use a foreach loop. Does javascript have something similar? How would I go about doing this?
JSON
{
  '.one' : 'AAA',
  '.two' : 'BBB'
}
Take a look at the each function of jQuery:
var map = { 
  '.one': 'AAA', 
  '.two': 'BBB' 
};
$.each(map, function(key, val) { 
  $(key).css('background-color', '#'+val);
});
Use the jQuery.each method
jQuery.each(JSONobject, function(key, value) {
    $(key).css('background-color', '#'+value);
});
You can also use this instead of value in the function, because the function is executed in the context of each element.
jQUery has an each function
$.each(yourJsonObject, function(key, value) {
    //whatever processing
});
This will work:
var data = {
  '.one' : 'AAA',
  '.two' : 'BBB'
};
for (var key in data)
    if (typeof(data[key]) == 'string')
        $(key).css('background-color', '#' + data[key]);
A JSFiddle demo:
http://jsfiddle.net/r7s96/
Using jquery
var data = {
    '.one' : 'AAA',
    '.two' : 'BBB'
};
$.each(data, function(key, value){
    $(key).css('background-color', '#' + value);
})
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论