<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>点击页面内容显示标签名</title>
</head>
<body>
<div>测试
<img src="hello.png" alt="hello" title="hello" >
<span>我是span</span>
<a >我是a</a>
</div>
<p>我是p</p>
</body>
<script>
let el=document.getElementsByTagName('body');
// console.log(el);
el[0].onclick=function(event){
let ev = event || window.event;
let selected = ev.target || ev.srcElement;
console.log(selected.tagName);
}
</script>
</html>
三个兼容的写法
1. oWidth = document.body.clientWidth || document.documentElement.clientWidth;
2. ev = event || window.event;
3. tagName = ev.target || ev.srcElement;
网友评论