Home >Web Front-end >JS Tutorial >Use each in jQuery to process json data_jquery

Use each in jQuery to process json data_jquery

WBOY
WBOYOriginal
2016-05-16 16:02:431049browse

eg: When the corresponding value of the passed-in ID is true, add a class named focus to the corresponding ID tag, such as:

var obj = { id01:'true', id02:'flase', id03:'true'};

$.each(obj,function(key,val){ 
   if(val == 'true'){ 
      $('#' + key).addClass('focus'); 
    } 
});

Result: A class tag will be added to the tags with IDs id01 and id03;

eg2:

The json data is as follows:

[ 
{"Id": 10004, "PageName": "club"}, 
{"Id": 10040, "PageName": "qaz"}, 
{"Id": 10059, "PageName": "beauty"}
]

If you want to use jquery to operate on this data, use $.each:

$.each(data, function(i, item) {
  alert(item.PageName);
});

I personally tested it and it works.

The above is the entire content of this article, I hope you all like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn