码迷,mamicode.com
首页 > Web开发 > 详细

js 点击列表li,获得当前li的id

时间:2018-12-17 14:19:05      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:fun   监听   cti   click   target   add   art   sdn   func   

html

<ul id="demo">
<li id="li-1">li1</li>
<li id="li-2">li2</li>
<li id="li-3">li3</li>
<li id="li-4">li4</li>
<li id="li-5">li5</li>
</ul>
1
2
3
4
5
6
7
js
第一种:监听事件

document.getElementById("demo").addEventListener("click",function(e){
console.log(e.target.id);
})
1
2
3
第二种

var ul = document.getElementById(‘demo‘);
ul.onclick = function(e) {
console.log(e.target.id);
};
1
2
3
4
第三种:jQuery点击事件

$(‘#demo‘).click(function(e){
console.log(e.target.id);
});
---------------------
作者:sulingliang
来源:CSDN
原文:https://blog.csdn.net/qq_32862143/article/details/73824563
版权声明:本文为博主原创文章,转载请附上博文链接!

js 点击列表li,获得当前li的id

标签:fun   监听   cti   click   target   add   art   sdn   func   

原文地址:https://www.cnblogs.com/apolloren/p/10130738.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!