日期:2014-05-16 浏览次数:20366 次
<div>sdgsdgo</div> <div>tert</div> <div>wsgs</div> <div>tert</div> <script type="text/javascript"> var odivs = document.getElementsByTagName("DIV"); for (var i = 0; i < odivs.length; i++) { odivs[i].onclick = function() { alert(this.innerHTML); } }
------解决方案--------------------
遍历dom,给dom添加click
<!DOCTYPE HTML> <html> <head> <meta charset="gb2312" /> <title></title> <style> </style> </head> <body> <div>123</div> <div>123</div> <div>123</div> <div>123</div> <script> function test(object){ var tag = object.firstChild.tagName; alert(tag) } var obj = document.getElementsByTagName('div'); for(var i = 0, len = obj.length; i < len; i++){ obj[i].onclick = function(){ test(this) } } </script> </body> </html>