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>