If you want the .childBox
id when clicking on .childBox
div you can just hook the click event on the .childBox
class:
(如果在单击.childBox
div时需要.childBox
id,则可以仅钩住.childBox
类上的click事件:)
$('.childBox').click(function (event) {
var id = $(this).attr('id');
console.log(id);
});
EDIT:
(编辑:)
If you want to access the .childBox
from .parentBox
event you can do this:
(如果要从.parentBox
事件访问.childBox
, .parentBox
可以执行以下操作:)
$('.parentBox').click(function (event) {
var id = $(this).find('.childBox').attr('id');
console.log(id);
});
When dynamically adding children it's a good idea to hook the event on the parent or on the document object like this:
(动态添加子级时,最好将事件挂接到父级或文档对象上,如下所示:)
$(document).on('click', '.childBox' , function() {
console.log($(this).attr('id'));
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…