本文介绍了如何从DIV类获取正确的DIV ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生,


我的同一个班级名称上有多个DIV,这是示例代码.

Dear Sir,


I Have multiple DIV on same class name, Here is the sample Code.

<div id = "1" class ="Item">A</div>
<div id = "2" class ="Item">B</div>
<div id = "3" class ="Item">C</div>
<div id = "4" class ="Item">D</div>



我需要div点击事件的JavaScript,在那种情况下,我需要使用Div类获取Div ID,

例如:当我单击第二个项目 Class Name = Item 时,我得到的ID为"2"


请帮帮我.

谢谢,

Dileep ..



I need javascript for div click event and in that event i need to get the Div ID using Div class,

For eg: When i clicked the Second Item Class Name = Item , then i got the ID as "2"


Please Help Me.

Thanks,

Dileep..

推荐答案

function myOnclick(el)
{
 alert(el.id);
}





<div id = "1" onclick='myOnclick(this);' class ="Item">A</div>
<div id = "2" onclick='myOnclick(this);' class ="Item">B</div>
<div id = "3"  onclick='myOnclick(this);'class ="Item">C</div>
<div id = "4"  onclick='myOnclick(this);'class ="Item">D</div>


这篇关于如何从DIV类获取正确的DIV ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:10
查看更多