how to find the div id on mouse click in jquery?
I have multiple div in a webform and when i clicked on a particular div i need to perform some task based on the div id.
Is there any way to find the div id on mouse click?
<table>
<tr>
<td>
<div id="firstdiv">div1</div>
</td>
<td>
<div id="seconddiv">div2</div>
<td>
<td>
<div id="thriddiv">div3</div>
</td>
<td>
<div id="fourthdiv">div4</div>
</td>
</tr>
</table>
请试试这个:
$("table div").click(function() {
var divID = this.id;//Or $(this).attr("id");
//Your code here
});
$('div').click(function() {
console.log($(this).attr('id'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>
<div id="firstdiv">div1</div>
<td>
<td>
<div id="seconddiv">div2</div>
<td>
<td>
<div id="thriddiv">div3</div>
<td>
<td>
<div id="fourthdiv">div4</div>
<td>
<tr>
<table>
click
event over div
element this
refers to clicked element in click-handler
id
property of the element
$('table div').on('click', function() {
console.log(this.id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table>
<tr>
<td>
<div id="firstdiv">div1</div>
</td>
<td>
<div id="seconddiv">div2</div>
</td>
<td>
<div id="thriddiv">div3</div>
</td>
<td>
<div id="fourthdiv">div4</div>
</td>
</tr>
</table>
链接地址: http://www.djcxy.com/p/83306.html
上一篇: 在表单提交中存储会话