gpt4 book ai didi

javascript - JS函数参数自行重置

转载 作者:行者123 更新时间:2023-12-02 14:10:38 25 4
gpt4 key购买 nike

我正在尝试创建一个简单的问答“闪存卡”程序。用户输入该单词在相反语言中的含义。一切工作正常,除了一件事:

当您单击按钮进入下一个级别时,它工作正常。然而,当你输入正确答案时,它又回到第一级。这是为什么呢? next() 函数 level 参数是否将自身重置回 1?请帮忙!

fiddle

HTML:

  </head>

<body>
<h1>Spanish</h1>
<div id="main">
<h1 id="topic_name">Subject Pronouns</h1>
<p>Type the word/phrase below in the opposite language</p>
<p>If there is an accent or ~ above a letter, put ^ before that letter. Example: diecis^eis</p>
<hr id="margin-bottom">
<h1 id="question"></h1>
<input id="answer_box"/>
<button id="next"></button>
<button id="answer">Show Answer</button>
</div>

</body>
</html>

JS:

$(document).ready(function(){

var lvl1=
[
[["I"],["yo"]],
[["you (formal)"],["usted"]],
[["you (informal)"],["t^u"]],
[["he"],["^el"]],
[["she"],["ella"]],
[["we (masculine)"],["nosotros"]],
[["we (feminine)"],["nosotras"]],
[["you all (formal)"],["ustedes"]],
[["you all (informal)"],["vosotros"]],
[["they (masculine or mixed)"],["ellos"]],
[["they (feminine)"],["ellas"]],
];

var lvl2=
[
[["yo"],["I"]],
[["usted"],["you (formal)"]],
[["t^u"],["you (informal)"]],
[["^el"],["he"]],
[["ella"],["she"]],
[["nosotros"],["we (masculine)"]],
[["nosotras"],["we (feminine)"]],
[["ustedes"],["you all (formal)"]],
[["vosotros"],["you all (informal)"]],
[["ellos"],["they (masculine)"]],
[["allas"],["they (feminine)"]],
];

var lvl3=
[
[["yo soy"],["I am"]],
[["tú eres"],["you (informal) are"]],
[["él es"],["he is"]],
[["ella es"],["she is"]],
[["usted es"],["you (formal) are"]],
[["nosotros somos"],["we are"]],
[["vosotros sois"],["you all (informal) are"]],
[["ellos/ellas son"],["they are"]],
[["ustedes son"],["you all (formal) are"]],
];

var lvl4=
[
[["I am"],["yo soy"]],
[["you (informal) are"],["t^u eres"]],
[["he is"],["^el es"]],
[["she is"],["ella es"]],
[["you (formal) are"],["usted es"]],
[["we are"],["nosotros somos"]],
[["you all (informal) are"],["vosotros sois"]],
[["you all (formal) are"],["ustedes son"]],
];

next(1);

function next(level){
random=(Math.floor(Math.random()*10));

switch(level){
case 1:
question=lvl1[random][0];
answer=lvl1[random][1];
$('#next').text("Level 2");
break;
case 2:
question=lvl2[random][0];
answer=lvl2[random][1];
$('#next').text("Level 3");
break;
case 3:
random=(Math.floor(Math.random()*9));
question=lvl3[random][0];
answer=lvl3[random][1];
$('#next').text("Level 4");
break;
case 4:
var random=(Math.floor(Math.random()*8));
question=lvl4[random][0];
answer=lvl4[random][1];
$('#next').text("Done");
break;
default:
alert("switch error");
}


$('#question').text(question);



$('#answer_box').keyup(function(){
if($(this).val()==answer){
$('#answer_box').attr("placeholder","");
$('#answer_box').val("");
next(level);
}
});



$('#next').click(function(){
next(level+1);
});



$('#answer').click(function(){
$('#answer_box').attr("placeholder",answer);
});


}//function next

});//end

CSS:

#main{
border:3px solid blue;
height:500px;
width:600px;
top:50%;
left:50%;
position:fixed;
margin-top:-230px;
margin-left:-300px;
border-radius:5%;
}

h1{
text-align:center;
}


p{
text-align:center;
}

#margin-bottom{
margin-bottom:80px;
}

#next{
display:block;
margin:0 auto;
}

#answer_box{
display:block;
margin:0 auto;
margin-bottom:20px;
}

#answer{
display:block;
margin:0 auto;
}

最佳答案

问题是未能声明变量并意外创建了闭包。

question=answer=正在创建同名的窗口属性,因为它们尚未声明。

level是函数 next 的形式参数首先设置为1通过声明next(1)

点击函数在 next 中声明为嵌套函数并访问存储在 level 参数中的值(当 next() 返回时创建一个闭包)。它们每次也会重新应用于按钮元素next被调用。

建议的解决方案:声明 question answerlevel “就绪”IIFE 中的变量。在 next 调用之外声明并应用按钮事件处理程序一次。维护level使用显式代码。

<小时/>更新详细信息:

删除

 next(1);

并替换为变量定义和不带参数的 next() 调用。级别在下一个函数之外保持。

var question = "";
var answer = "";
var level = 1;
next();

function next(level)... 中删除 level 参数并替换为

function next(){
random=(Math.floor(Math.random()*10));

switch(level){
case 1:
question=lvl1[random][0];
answer=lvl1[random][1];
$('#next').text("Level 2");
break;
case 2:
question=lvl2[random][0];
answer=lvl2[random][1];
$('#next').text("Level 3");
break;
case 3:
random=(Math.floor(Math.random()*9));
question=lvl3[random][0];
answer=lvl3[random][1];
$('#next').text("Level 4");
break;
case 4:
var random=(Math.floor(Math.random()*8));
question=lvl4[random][0];
answer=lvl4[random][1];
$('#next').text("Done");
break;
default:
alert("switch error");
}

$('#question').text(question);


}// end of function next body

这与发布的代码基本相同,但将 keyup 和下一个按钮处理程序移至 function next 后面 body ;不要在其中定义它们:

$('#next').click(function(){
level = level + 1; // increase level
next();
});

$('#answer_box').keyup(function(){
if($(this).val()==answer){
$('#answer_box').attr("placeholder","");
$('#answer_box').val("");
next(); // stay on current level
}
});

我还没有添加代码来处理当显示“完成”时单击下一步按钮时会发生什么情况。您询问的闭包是通过注册在 next 内创建的 keyup 和 click 处理程序匿名函数来创建的。函数:匿名函数在 next 内查找变量和参数定义,对应于创建匿名函数时它们的值。如果您不熟悉这个概念,请搜索 javascript 中的闭包。

关于javascript - JS函数参数自行重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39628590/

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