gpt4 book ai didi

Javascript 函数不起作用,控制台显示它未定义

转载 作者:行者123 更新时间:2023-12-02 17:24:43 26 4
gpt4 key购买 nike

我有一个用 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;
}
}

最佳答案

我运行你的脚本,得到: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;
}
}

关于Javascript 函数不起作用,控制台显示它未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23585372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com