JS函数声明里面有参数,但是运行时却没有明显传参进去,期间发生了什么事情? 10
代码如下:btnBu.addEventListener(LMouseEvent.MOUSE_UP,onclick);functiononclick(event,displ...
代码如下:
btnBu.addEventListener(LMouseEvent.MOUSE_UP,onclick);
function onclick(event,display){
var selfValue,enemyValue;
if(display.name == "shitou"){
selfValue = 0;
}else if(display.name == "jiandao"){
selfValue = 1;
}else if(display.name == "bu"){
selfValue = 2;
}
enemyValue = Math.floor(Math.random() * 3);
selfBitmap.bitmapData = showList[selfValue];
enemyBitmap.bitmapData = showList[enemyValue];
var result = checkList[selfValue][enemyValue];
if(result == -1){
loss += 1;
}else if(result == 1){
win += 1;
}else{
draw += 1;
}
console.log(win + loss + draw);
selfTextWin.text = "胜利次数:" + win;
selfTextLoss.text = "失败次数:" + loss;
selfTextDraw.text = "平局次数:" + draw;
selfTextAll.text = "猜拳次数:" + (win + loss + draw);
}
上面的函数onclick(event,display)中display的值究竟是如何被传递进去的? 展开
btnBu.addEventListener(LMouseEvent.MOUSE_UP,onclick);
function onclick(event,display){
var selfValue,enemyValue;
if(display.name == "shitou"){
selfValue = 0;
}else if(display.name == "jiandao"){
selfValue = 1;
}else if(display.name == "bu"){
selfValue = 2;
}
enemyValue = Math.floor(Math.random() * 3);
selfBitmap.bitmapData = showList[selfValue];
enemyBitmap.bitmapData = showList[enemyValue];
var result = checkList[selfValue][enemyValue];
if(result == -1){
loss += 1;
}else if(result == 1){
win += 1;
}else{
draw += 1;
}
console.log(win + loss + draw);
selfTextWin.text = "胜利次数:" + win;
selfTextLoss.text = "失败次数:" + loss;
selfTextDraw.text = "平局次数:" + draw;
selfTextAll.text = "猜拳次数:" + (win + loss + draw);
}
上面的函数onclick(event,display)中display的值究竟是如何被传递进去的? 展开
展开全部
这个display是当前空间自身,相当于this
onclick可以动态传参
Html代码
复制代码 代码如下:
<body>
<input id="certid" type="text" value="123456" >
<input id="btn" type="button" value="button" onclick="">
</body>
Javascript代码
复制代码 代码如下:
<script>
function show(value1,value2)
{
alert(value1+","+value2);
}
var i = 10;
btn.onclick = function(){
show(certid.value,i);
};
<script>
这样就实现了动态给onclick事件句柄赋值,并支持参数的传递.
详细出处参考:http://www.jb51.net/article/35107.htm
onclick可以动态传参
Html代码
复制代码 代码如下:
<body>
<input id="certid" type="text" value="123456" >
<input id="btn" type="button" value="button" onclick="">
</body>
Javascript代码
复制代码 代码如下:
<script>
function show(value1,value2)
{
alert(value1+","+value2);
}
var i = 10;
btn.onclick = function(){
show(certid.value,i);
};
<script>
这样就实现了动态给onclick事件句柄赋值,并支持参数的传递.
详细出处参考:http://www.jb51.net/article/35107.htm
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询