Javascript 当父div事件处理程序订阅该类时,如何获取子元素的id
因此,我有一个类似以下的div:Javascript 当父div事件处理程序订阅该类时,如何获取子元素的id,javascript,jquery,html,events,Javascript,Jquery,Html,Events,因此,我有一个类似以下的div: <div class="parentclass" id="parent"> <select id="child1"></select> <select id="child2"></select> ... </div> 当我更改子选项时,这会提醒父项的id。所以我试图提醒孩子id。我知道我可以使用jquery通配符,但还有其他方法吗 提前感谢您的帮助。从对象获取目标元
<div class="parentclass" id="parent">
<select id="child1"></select>
<select id="child2"></select>
...
</div>
当我更改子选项时,这会提醒父项的id。所以我试图提醒孩子id。我知道我可以使用jquery通配符,但还有其他方法吗
提前感谢您的帮助。从对象获取目标元素,对象可以作为回调的第一个参数访问
$('.parentclass').on('change', function(event) {
alert(event.target.id);
});
$('.parentclass')。关于('change',函数(事件){
警报(event.target.id);
});代码>
1.
1.
1.
1.
1.
1.
1.
1.
1.
1.
1.
$('select.select')。关于('change',函数(事件){
警报($(this.attr('id'));
});代码>
1.
1.
1.
1.
1.
1.
1.
1.
1.
1.
1.
为什么不使用类来选择和编写js一旦更改此类,警报($(this).attr('id'))您只需像$('.parentclass select')一样使用即可。
。这将使您在下拉列表id
$('.parentclass select')。在('change',function(){
警报(this.id);
});代码>
选项1
选项1 2
选项2
选项2 1
非常感谢我所说的needed@Maxqueue:很乐意帮忙:)
$('.parentclass').on('change', function(event) {
alert(event.target.id);
});