获取div中元素的ID [英] getting id of elements inside the div

查看:245
本文介绍了获取div中元素的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<div id="maindiv">
    <DIV id="first">first div<DIV>
    <DIV id="second">second div</DIV>
    <input id="input" type="text"/>
    <button id="button" value="send"></button>
</div>

当使用jQuery单击内部div之一时,我想获取主div内的div的ID.该怎么办?

I want to get the id of the div inside the main div when one of the inner div is clicked using jquery. How can this be done?

推荐答案

$('#maindiv div').on('click', function(e) { // or '#maindiv > div' for proper children
    e.stopPropagation(); // or else this will bubble up through the DOM
    var my_id = $(this).attr('id');
    alert(my_id);
});​

http://jsfiddle.net/Rn9Bu/

这篇关于获取div中元素的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆