JavaScript中,如何在点击事件里面重新调用一下window.onload函数

Hell* 2019-03-20 05:16:13
这是我的window.onload函数
window.onload = function initGame() {
//初始化游戏函数
if (this.level < 4) {
this.margin = 12;
}
else if (this.level < 8) {
this.margin = 6;
}
else if (this.level < 16) {
this.margin = 3;
}
else {
this.margin = 1;
}
this.card = [];
this.size = (600 - (this.level + 1) * this.margin) / this.level;
for (var i = this.level * this.level; i--;) {
this.card.push({
color: false,//false是黄色,true是蓝色
})
}
//动态生成DIV
for (var x in card) {
var box = document.createElement("div");
box.style.width = size + 'px';
box.style.height = size + 'px';
box.style.marginTop = margin + 'px';
box.style.marginLeft = margin + 'px';
box.style.marginBottom = margin + 'px';
box.style.marginRight = margin + 'px';
box.style.background = '#E6AB5E';
box.style.cssFloat = 'left';
box.className += ' ' + 'card';
box.id += '' + x;
var bo = document.getElementById("board");
bo.appendChild(box);
for (var j = 1; j < this.level; j++) {
if (x == j * this.level) {
box.style.clear = 'both';//清除浮动效果
}
}
var id = x;
var e = document.getElementById(id);
e.setAttribute("onclick", "check(id)");
}
}


我想在我的点击事件里面再调用一下这个函数,下面是我写的点击事件
function check(x) {
//this.card[x].color = !this.card[x].color;
var id = document.getElementById(x);
if (id.style.background == "rgb(230, 171, 94)") {
id.style.background = '#5C90FF';
} else {
id.style.background = '#E6AB5E';
}
changeNeighbor(x);
gameOver();
if(gameOver()){
setTimeout(function(){
alert('恭喜通过第'+this.level+'关');
this.level++;
this.initfame();
//initGame();
},200)
}
}


重点是红色那里,该怎么调用呀?求大神指导一下
...全文
302 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
kuanhuayu 2019-03-21
  • 打赏
  • 举报
回复
function test() { alert('test'); } window.onload = test; checkclick(){ test(); }
Hell* 2019-03-20
  • 打赏
  • 举报
回复
<span style="color: #FF0000;">this.initfame();</span>
这句话发错了应该是 this.initfame(); 多余的是CSDN自动给我加上的

我用的CSDN字体变红的功能,咋给我加代码呢?

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧