日期:2014-05-16 浏览次数:20500 次
function test(obj){
obj.onclick = function(obj){alert(obj);};
}
function test(obj){
obj.onclick = function(obj){alert(obj);}(obj);
}
<!DOCTYPE HTML>
<html>
<head>
<meta charset="gb2312" />
<title></title>
<style>
</style>
</head>
<body>
<div id="test">123</div>
<script>
function test(obj){
obj.onclick = function(){
alert(obj.innerHTML);
};
}
test( document.getElementById('test') )
</script>
</body>
</html>
------解决方案--------------------
楼主的写法首先说一下 是不严格的 你的参数都叫同一个名字 很容易把自己绕进去
给你列了2种 你看你要哪一种:
1、obj都一样
function test(obj){
obj.onclick = function(e){
alert(this);//this就是你的obj这个dom对象
};
}