HLJ 发布于
2021-12-09 09:57:54

原生js 点击获取当前标签的内容

通过 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>
当前文章内容为原创转载请注明出处:http://www.good1230.com/detail/2021-12-09/569.html
最后生成于 2023-06-27 21:38:02
此内容有帮助 ?
0