2021-12-09 09:57:54
通过 js原生代码
事件获取当前标签的属性值的实例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<ul>
<li id="1">11111111111</li>
<li id="2">22222222222</li>
<li id="3">33333333333</li>
<li id="4">44444444444</li>
<li id="5">55555555555</li>
<li id="6">66666666666</li>
</ul>
</body>
<script>
let ul = document.querySelector('ul')
let li =ul.querySelectorAll('li')
for(let i = 0; i < li.length; i++){
li[i].onclick = function(e){
console.log(e.target.id)
console.log(e.target.innerHTML)
}
}
</script>
</html>
最后生成于 2023-06-27 21:38:02
热门推荐: