简体   繁体   English

如何从单击事件获取对象属性

[英]How to get the object properties from click event

HTML HTML

<ol id="person-list"></ol>
<div id="person">
    <h2 id="person-name"></h2>
    <div id="person-age"></div>
    <img id="person-address">
</div>

JS JS

var persons = [{
    name: 'John',
    age: 54,
    address: '123 Main st.'
}, {
    name: 'Jack',
    age: 55,
    address: '456 Oak Ln.'
}];


$('#person-list').on('click', 'li', function () {
    alert('Hello'); // get other object info here?
});

var f = function () {
    for(var i = 0, len = persons.length; i < len; i++) {
        $('#person-list').append('<li>' + persons[i].name + '</li>');
    }
};
f();

When a person's name is clicked, it would need to grab the appropriate object's other properties and place them in the appropriate divs. 单击某个人的名字时,将需要获取相应对象的其他属性,并将它们放在适当的div中。 For example, if John is clicked, John's age and address need to show in the divs below. 例如,如果单击John,则John的年龄和地址需要显示在下面的div中。 How could I link the person's name in the list to its other property values? 如何将列表中的人名链接到其其他属性值? I thought about saving the value "i" with every name during the for-loop so there's a number associated with each name. 我考虑过在for循环中为每个名称保存值“ i”,因此每个名称都有一个数字。 Then somehow reference person[i].age when the "i"th value is clicked, but that sounded clumsy. 然后,当单击第“ i”个值时,以某种方式引用人[i] .age,但这听起来很笨拙。

JSFIDDLE 的jsfiddle

Use data-* attribute to store the index on the li . 使用data-*属性将索引存储在li

  1. Use data-index on the li when creating and store the index i on it 创建时在li上使用data-index并在其上存储索引i
  2. Use jQuery's data('index') to retrieve the saved index on the clicked element 使用jQuery的data('index')来获取clicked元素上保存的索引
  3. Use $(this) to access the clicked element in event handler 使用$(this)访问事件处理程序中的clicked元素

Demo 演示

 var persons = [{ name: 'John', age: 54, address: '123 Main st.' }, { name: 'Jack', age: 55, address: '456 Oak Ln.' }]; $('#person-list').on('click', 'li', function() { // Get the clicked person's information var person = persons[$(this).data('index')]; // Set info in the corresponding elements $('#person-name').html(person.name); $('#person-age').html(person.age); $('#person-address').html(person.address); }); var f = function() { for (var i = 0, len = persons.length; i < len; i++) { $('#person-list').append('<li data-index="' + i + '">' + persons[i].name + '</li>'); // ^^^^^^^^^^^^^^^^^^^^^^ } }; f(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ol id="person-list"></ol> <div id="person"> <h2 id="person-name"></h2> <div id="person-age"></div> <div id="person-address"></div> </div> 

您可以将data-attribute添加到<li data-person="id_of_the_person">元素,并在click事件处理程序中使用$(this).data("person")进行获取,然后从列表中获取该人。

You need to use the .index() function to get the index of the name and then use that index to get the name from the persons array. 您需要使用.index()函数来获取名称的索引,然后使用该索引来从person数组中获取名称。 Like this: 像这样:

$('#person-list').on('click', 'li', function () {

    alert(persons[$(this).index()].name)
});

And here's the updated fiddle . 这是更新的小提琴 From there, you can do all the text manipulations you need. 从那里,您可以进行所需的所有文本操作。

$('#person-name').html(persons[$(this).index()].name);
$('#person-age').html(persons[$(this).index()].age);
$('#person-address').html(persons[$(this).index()].address);

In event handler you can access the index() and then you can access the appropriate data. 在事件处理程序中,您可以访问index() ,然后可以访问适当的数据。

Here is JSFiddle 这是JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM