我有一个用Javascript和我的html文件制作的函数
<script type="text/javascript" src="javascript.js"></script>
在里面。该页面上的Javascript函数不起作用,控制台说该函数未定义。我想知道我做错了什么,任何意见都会非常感激。
var player = 0;
var xvalue = 0;
var ovalue = 0;
function tic(a)
{
if(player === 0)
{
document.getElementById(a).value = 'X';
document.getElementById(a).disabled = 'true';
player = 1;
if(a === 'aa'){
var i = 1;
for(i = 1; i <= 1; i++; ){
xvalue++;
}
}
else if(a === 'ab'){
var i = 1;
for(i = 1; i <= 2; i++; ){
xvalue++;
}
}
else if(a === 'ac'){
var i = 1;
for(i = 1; i <= 4; i++; ){
xvalue++;
}
}
else if(a === 'ba'){
var i = 1;
for(i = 1; i <= 8; i++; ){
xvalue++;
}
}
else if(a === 'bb'){
var i = 1;
for(i = 1; i <= 16; i++; ){
xvalue++;
}
}
else if(a === 'bc'){
var i = 1;
for(i = 1; i <= 32; i++; ){
xvalue++;
}
}
else if(a === 'ca'){
var i = 1;
for(i = 1; i <= 64; i++; ){
xvalue++;
}
}
else if(a === 'cb'){
var i = 1;
for(i = 1; i <= 128; i++; ){
xvalue++;
}
}
else if(a === 'cc'){
var i = 1;
for(i = 1; i <= 256; i++; ){
xvalue++;
}
}
if(xvalue === 448){
alert("player 1 wins!");
}
else if(xvalue === 7){
alert("player 1 wins!");
}
else if(xvalue === 56){
alert("player 1 wins!");
}
else if(xvalue === 273){
alert("player 1 wins!");
}
else if(xvalue === 84){
alert("player 1 wins!");
}
else if(xvalue === 73){
alert("player 1 wins!");
}
else if(xvalue === 146){
alert("player 1 wins!");
}
else if(xvalue === 292){
alert("player 1 wins!");
}
}
else
{
document.getElementById(a).value = 'O';
document.getElementById(a).disabled = 'true';
player = 0;
}
}
答案 0 :(得分:1)
我运行你的脚本我得到:SyntaxError: Unexpected token ;
所以,你在循环中出错:
for(i = 1; i <= 256; i++; ){
正确的方法是:
for(i = 1; i <= 256; i++ ){
撤消;
因此,正确的代码是:
var player = 0;
var xvalue = 0;
var ovalue = 0;
function tic(a)
{
if(player === 0)
{
document.getElementById(a).value = 'X';
document.getElementById(a).disabled = 'true';
player = 1;
if(a === 'aa'){
var i = 1;
for(i = 1; i <= 1; i++ ){
xvalue++;
}
}
else if(a === 'ab'){
var i = 1;
for(i = 1; i <= 2; i++ ){
xvalue++;
}
}
else if(a === 'ac'){
var i = 1;
for(i = 1; i <= 4; i++ ){
xvalue++;
}
}
else if(a === 'ba'){
var i = 1;
for(i = 1; i <= 8; i++ ){
xvalue++;
}
}
else if(a === 'bb'){
var i = 1;
for(i = 1; i <= 16; i++ ){
xvalue++;
}
}
else if(a === 'bc'){
var i = 1;
for(i = 1; i <= 32; i++ ){
xvalue++;
}
}
else if(a === 'ca'){
var i = 1;
for(i = 1; i <= 64; i++ ){
xvalue++;
}
}
else if(a === 'cb'){
var i = 1;
for(i = 1; i <= 128; i++ ){
xvalue++;
}
}
else if(a === 'cc'){
var i = 1;
for(i = 1; i <= 256; i++ ){
xvalue++;
}
}
if(xvalue === 448){
alert("player 1 wins!");
}
else if(xvalue === 7){
alert("player 1 wins!");
}
else if(xvalue === 56){
alert("player 1 wins!");
}
else if(xvalue === 273){
alert("player 1 wins!");
}
else if(xvalue === 84){
alert("player 1 wins!");
}
else if(xvalue === 73){
alert("player 1 wins!");
}
else if(xvalue === 146){
alert("player 1 wins!");
}
else if(xvalue === 292){
alert("player 1 wins!");
}
}
else
{
document.getElementById(a).value = 'O';
document.getElementById(a).disabled = 'true';
player = 0;
}
}
答案 1 :(得分:1)
For循环的语法是
for(initialization;condition;increment)
您在每个for
循环中使用了错误的语法
所以在增加后删除;
for(i = 1; i <= 64; i++; )
到
for(i = 1; i <= 64; i++)