冒泡原理实现,废话不多说,直接上代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>点击显示标签名</title>
</head>
<body>
<div>测试
<span>我是span</span>
<a >我是a</a>
</div>
<p>我是p</p>
</body>
<script>
var el = document.getElementsByTagName('body');
el[0].onclick = function(event){
var ev = event || window.event;//兼容普通浏览器和ie(ie8及以下没有event,只有window.event)
var selected = ev.target || ev.srcElement;//兼容
alert(selected.tagName);
}
</script>
</html>