很菜的一个问题.望各位大神帮下,function test(obj){
obj.onclick = function(obj){alert(obj);};
}就是这么简单的一个问题.怎么传参? 这样传是无效的.而如果用function test(obj){
obj.onclick = function(obj){alert(obj);}(obj);
}这样也是不行的,这样会立即执行.求教
obj.onclick = function(obj){alert(obj);};
}就是这么简单的一个问题.怎么传参? 这样传是无效的.而如果用function test(obj){
obj.onclick = function(obj){alert(obj);}(obj);
}这样也是不行的,这样会立即执行.求教
解决方案 »
- 滑动门一个问题,鼠标经过DIV层时,如何自动触发这一个层?调了好久没有弄出来。
- jquery1.4之后:contains(text)函数的问题
- 后台用ScriptManager.RegisterClientScriptBlock,调前台JS,找不到控件ID怎么办啊?有没有好的解决办法?
- 这个js竟然FF支持,IE不支持?
- 小弟求段小代码。高人请进!
- 做了一个群发信息的Demo测试一下
- 关于文件绝对路径
- 如何用javaScript实现form的submit功能
- 能不能實現直接拖動這個層<div><iframe src=”http://#” width=200 height=200 frameborder=0></iframe></div>?
- Javascript组件库,学习javascript不妨看看这个东东
- 一个页面加载效果的想法,求实现。
- 帮忙看一个程序
<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>
这样如果这样写的话
obj.onclick = function(obj){alert(obj);};function(obj) obj 是传递的鼠标事件
1、obj都一样
function test(obj){
obj.onclick = function(e){
alert(this);//this就是你的obj这个dom对象
};
}2、obj不一样 这种情况是需要闭包来解决的function test(aaa){
var obj = document.getElementById('元素的id');
obj.onclick = (function(param){
return function(e){
alert(param);//这哥param就是test函数的形参aaa
alert(e);//这个就是该dom元素的event事件
};
})(aaa)
}}
var param="test";
obj.onclick = function(){
clickEvent(param);
};
}
function clickEvent(param)
{
alert(param);
}
第二种方式:
function test(obj){
obj.pamam="test";
obj.onclick =clickEvent;
}
function clickEvent()
{
alert(this.pamam);
}
function test(obj){
var param="test";
obj.onclick = function(){alert(param);};//局部函数可以直接访问该局部的变量
}
obj.onclick = function(){alert(obj);};
}
这个不用传参数了