Javascript 我应该将<;脚本>;新板()</脚本>;?
我有个问题,我需要把新黑板放在黑板上;在我的JavaScript文件中。我应该把它放在哪里,怎么称呼它?还有一个问题:有人能用这个代码写一个对象吗 非常感谢你的帮助 这是HTML文件Javascript 我应该将<;脚本>;新板()</脚本>;?,javascript,html,Javascript,Html,我有个问题,我需要把新黑板放在黑板上;在我的JavaScript文件中。我应该把它放在哪里,怎么称呼它?还有一个问题:有人能用这个代码写一个对象吗 非常感谢你的帮助 这是HTML文件 <html> <head> <link href="memory.css" rel="stylesheet"> <script src="memory.js"></script> </head> <body>
<html>
<head>
<link href="memory.css" rel="stylesheet">
<script src="memory.js"></script>
</head>
<body>
<h1>Koen's Heineken Memory Game:</h1>
<div id="memory_board"></div>
<script>newBoard();</script>
</body>
</html>
Koen的喜力记忆游戏:
新板();
这是.js文件
var memory_content = ['A','A','B','B','C','C','D','D','E','E','F','F','G','G','H','H','I','I','J','J','K','K','L','L'];
var memory_values = [];
var memory_tile_ids = [];
var cards_turned = 0;
Array.prototype.memory_tile_shuffle = function(){
var i = this.length, j, temp;
while(--i > 0){
j = Math.floor(Math.random() * (i+1));
temp = this[j];
this[j] = this[i];
this[i] = temp;
}
}
function newBoard(){
cards_turned = 0;
var output = '';
// De array wordt geschud.
memory_content.memory_tile_shuffle();
for(var i = 0; i < memory_content.length; i++){
output += '<div id="tile_'+i+'" onclick="memoryFlipTile(this,\''+memory_content[i]+'\')"></div>';
}
document.getElementById('memory_board').innerHTML = output;
}
function memoryFlipTile(tile,val){
// Kijkt of de kaart nog niet is omgedraaid.
if(tile.innerHTML == "" && memory_values.length < 2){
// De achtergrond van de kaart.
tile.style.background = 'white';
tile.innerHTML = val;
if(memory_values.length == 0){
memory_values.push(val);
memory_tile_ids.push(tile.id);
} else if(memory_values.length == 1){
memory_values.push(val);
memory_tile_ids.push(tile.id);
if(memory_values[0] == memory_values[1]){
cards_turned += 2;
memory_values = [];
memory_tile_ids = [];
if(cards_turned == memory_content.length){
alert("Goed gedaan! Ik zet een nieuw spel voor je klaar.");
document.getElementById('memory_board').innerHTML = "";
newBoard();
}
} else {
function flip2Back(){
var tile_1 = document.getElementById(memory_tile_ids[0]);
var tile_2 = document.getElementById(memory_tile_ids[1]);
tile_1.style.background = 'url(heineken.jpg) no-repeat';
tile_1.style.backgroundSize = '111px 111px';
tile_1.innerHTML = "";
tile_2.style.background = 'url(heineken.jpg) no-repeat';
tile_2.style.backgroundSize = '111px 111px';
tile_2.innerHTML = "";
memory_values = [];
memory_tile_ids = [];
}
(flip2Back, 750);
}
}
}
}
var memory_content=['A','A','B','B','C','C','D','D','E','E','F','F','G','G','H','H','H','I','J','K','K','K','L','L'];
变量内存_值=[];
变量内存_tile_id=[];
var值=0;
Array.prototype.memory\u tile\u shuffle=函数(){
var i=该长度,j,温度;
而(--i>0){
j=数学地板(数学随机()*(i+1));
温度=此[j];
这个[j]=这个[i];
此[i]=温度;
}
}
函数newBoard(){
卡片数量=0;
var输出=“”;
//De array wordt geschud。
memory_content.memory_tile_shuffle();
对于(变量i=0;i
您可以在加载时调用该函数:-
<body onload="newBoard()">
<h1>Koen's Heineken Memory Game:</h1>
<div id="memory_board"></div>
</body>
Koen的喜力记忆游戏:
像这样做
(function(){
// add all your methods and declaration here
newBoard();
})();
<html>
<head>
<link href="memory.css" rel="stylesheet">
</head>
<body>
<h1>Koen's Heineken Memory Game:</h1>
<div id="memory_board"></div>
<script src="memory.js"></script>
</body>
</html>
像这样更改memory.js
(function(){
// add all your methods and declaration here
newBoard();
})();
<html>
<head>
<link href="memory.css" rel="stylesheet">
</head>
<body>
<h1>Koen's Heineken Memory Game:</h1>
<div id="memory_board"></div>
<script src="memory.js"></script>
</body>
</html>
然后像这样更改html
(function(){
// add all your methods and declaration here
newBoard();
})();
<html>
<head>
<link href="memory.css" rel="stylesheet">
</head>
<body>
<h1>Koen's Heineken Memory Game:</h1>
<div id="memory_board"></div>
<script src="memory.js"></script>
</body>
</html>
Koen的喜力记忆游戏:
如果您想在页面加载后调用它,那么window.onload=newBoard()
您可以将其放置在html中的任何位置,但将其放置在其他
标记下面会更方便readable@TilwinJS是错误的。它必须是window.onload=newBoard
或window.onload=function(){newBoard();}代码>@usandfriends right。。我从问题中复制粘贴了函数名…只要在必要的DOM元素存在后调用函数,就应该没有什么区别。