[英]How to get data from an object using jQuery?
I am using an object to store data. 我正在使用一个对象来存储数据。 How can I get the data for the active person, then when a link is clicked, get it for the person who's link has been clicked?
我如何获取活动人员的数据,然后单击链接时获取链接人员的数据?
HTML 的HTML
<div id="people">
<a href="#steve" class="active">Steve</a>
<a href="#mike">Mike</a>
<a href="#cindy">Cindy</a>
</div>
JavaScript 的JavaScript
var $people = $('#people').find('a'),
activePerson = $people.hasClass('active'),
people = {
steve: [{
color: 'blue',
pet: 'bird'
}],
mike: [{
color: 'maroon',
pet: 'dog'
}],
cindy: [{
color: 'pink',
pet: 'snake'
}]
};
console.log(people.activePerson);
$people.click(function (e) {
e.preventDefault();
activePerson = $(this).attr('href').slice(1);
console.log(people.activePerson);
});
You can select them in your array like this: 您可以像这样在数组中选择它们:
JSFIDDLE: http://jsfiddle.net/L5sHS/ JSFIDDLE: http : //jsfiddle.net/L5sHS/
var $people = $('#people').find('a'),
activePerson = $people.hasClass('active'),
people = {
steve: [{
color: 'blue',
pet: 'bird'
}],
mike: [{
color: 'maroon',
pet: 'dog'
}],
cindy: [{
color: 'pink',
pet: 'snake'
}]
};
console.log(people.activePerson);
$people.click(function (e) {
e.preventDefault();
activePerson = $(this).attr('href').slice(1);
console.log(people[activePerson][0].color);
});
In this example I print out the color of the clicked person. 在此示例中,我打印出被点击的人的颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.