Java教程

JavaScript事件属性event.target

本文主要是介绍JavaScript事件属性event.target,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

JavaScript事件属性event.target

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>hahahhahahha</title>
  </head>
  <style>
    #main {
      width: 200px;
      height: 100px;
      background: red;
    }
  </style>
  <script>
    window.onload = function () {
      document.getElementById("main").onclick = function (e) {
        console.log(e.target);  //<div id="main" class="sb js node"><span>我爱JavaScript</span></div>
        console.log(e.target.id); //main
        console.log(e.target.tagName);  //div
        console.log(e.target.nodeName);  //div
        console.log(e.target.classList);  
        console.log(e.target.className);//sb js node
        console.log(e.target.innerHTML);  //<span>我爱JavaScript</span>
        console.log(e.target.innerText);  //我爱JavaScript
      };
    };
  </script>

  <body>
    <div id="main" class="sb js node"><span>我爱JavaScript</span></div>
  </body>
</html>

这篇关于JavaScript事件属性event.target的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!