1

I have this code below:

$('.name').filter(function(){
    var data = $(this);
    name = data.text();

    json.name = name;
})

This will return the text from this div:

<a href="javascript:void(0)" class="name">This is a name</a>

Basically on the page there around 20 photos of people, so there are around 20 or so <a> tags on the page with a class name of name. How would I loop through each <a> tag and produce a JSON variable which would contain something like this below

{ [id: 0, name: 'name 1'],[id: 1, name: 'name 2'] }

At the moment I can only get it to hold the value of one name, which is useless to me at the moment!

Any help appreciated!

4

1 回答 1

2

看来您有可用的 jQuery,这稍微简化了任务。使用.each()

var myList = [];
var id = 0;
$('name').each(function() {
  var name = $(this).text();
  myList.push({id: id, name: name});
  id = id + 1;
}
console.log(myList);
于 2014-09-15T09:38:01.787 回答