使用HTML、JavaScript和JQuery,如何通过单击为变量赋值?
我正在尝试使用HTML、JavaScript和JQuery(和CSS)制作一个“选择你自己的冒险”游戏。我希望能够为玩家的角色设置值,例如性别(男性/女性)、头发颜色(黑色、棕色、金色、红色)等。目前,我有使用HTML、JavaScript和JQuery,如何通过单击为变量赋值?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在尝试使用HTML、JavaScript和JQuery(和CSS)制作一个“选择你自己的冒险”游戏。我希望能够为玩家的角色设置值,例如性别(男性/女性)、头发颜色(黑色、棕色、金色、红色)等。目前,我有或如下所示: 示例代码: <h3 id="gender">Are you a <a href="#" id="boy" class="genderchoice">boy</a> or a <a href="#" id="girl" class=
或如下所示:
示例代码:
<h3 id="gender">Are you a <a href="#" id="boy" class="genderchoice">boy</a> or a <a href="#" id="girl" class="genderchoice">girl</a>?</h3>
我四处窥探了一下,认为这个设置可能会起作用,但显然不行,值一直没有定义。我在脚本的前面定义了变量,就在JQuery文档之后。准备就绪:
$(document).ready(function(){
var gendervalue;
$(otherJquery).things
同样,我在一个类似的问题中看到了这一点,但在本例中可能不起作用。您可以将onclick事件处理程序分配给将触发函数的按钮,将black的值分配给头发等。
正如@xufox在评论部分中提到的,您可以使用localstorage
在本地保存这些值,然后在需要时进行检索,这可以在代码中添加save Game
功能。如果你能提供你的代码,那么所有这些都可以通过它来解释
使用纯JavaScript:
var genderValue;
var boy = document.getElementById("boy");
var girl = document.getElementById("girl");
boy.onclick = myfunc;
girl.onclick = myfunc;
function myfunc(){
if(this.id == "girl"){
genderValue = "female";
}else{
genderValue = "male";
}
}
$('#boy').on('click',function(){
var genderValue = "male";
});
$('#girl').on('click',function(){
var genderValue = "female";
});
if(localStorage.getItem("genderValue") == null){
$('#boy').on('click',function(){
var genderValue = "male";
localStorage.setItem("genderValue","male")
});
$('#girl').on('click',function(){
var genderValue = "female";
localStorage.setItem("genderValue","female");
});
}else{
var genderValue = localStorage.getItem("genderValue");
}
带有jQuery代码:
var genderValue;
var boy = document.getElementById("boy");
var girl = document.getElementById("girl");
boy.onclick = myfunc;
girl.onclick = myfunc;
function myfunc(){
if(this.id == "girl"){
genderValue = "female";
}else{
genderValue = "male";
}
}
$('#boy').on('click',function(){
var genderValue = "male";
});
$('#girl').on('click',function(){
var genderValue = "female";
});
if(localStorage.getItem("genderValue") == null){
$('#boy').on('click',function(){
var genderValue = "male";
localStorage.setItem("genderValue","male")
});
$('#girl').on('click',function(){
var genderValue = "female";
localStorage.setItem("genderValue","female");
});
}else{
var genderValue = localStorage.getItem("genderValue");
}
您必须将genderValue
的值分配给字符串,而字符串总是在引号内,这就是错误所在
对于本地存储
:
var genderValue;
var boy = document.getElementById("boy");
var girl = document.getElementById("girl");
boy.onclick = myfunc;
girl.onclick = myfunc;
function myfunc(){
if(this.id == "girl"){
genderValue = "female";
}else{
genderValue = "male";
}
}
$('#boy').on('click',function(){
var genderValue = "male";
});
$('#girl').on('click',function(){
var genderValue = "female";
});
if(localStorage.getItem("genderValue") == null){
$('#boy').on('click',function(){
var genderValue = "male";
localStorage.setItem("genderValue","male")
});
$('#girl').on('click',function(){
var genderValue = "female";
localStorage.setItem("genderValue","female");
});
}else{
var genderValue = localStorage.getItem("genderValue");
}
localStorage
用于在用户计算机上保存值。如果您的游戏需要很多天才能完成,那么此功能非常有用。在上面的代码中,如果用户不是第一次访问,将从用户的计算机中检索genderValue
。但是按钮仍然可见,因为我们没有编写代码来隐藏它们。如果你想要这个功能,请写在评论部分
有关详细说明和用法,请参阅
尝试jquery单击处理程序和选择器:
var-gendervalue;
$(函数(){
$('a.genderchoice')。在('click',function()上{
gendervalue=this.id==“男孩”?“男性”:“女性”;
警报(gendervalue);
});
});代码>
您是a还是a?
欢迎来到Stack Overflow!请回顾有关堆栈溢出的问题,以及您想要的问题类型和类型?这是一个很好的问题,但它有点宽泛,需要回答者做大量工作。如果您发布一些您尝试过的代码的简短部分,并让我们知道什么不适合您,您可能会得到更多帮助?谢谢!现在我想知道,这可能是一个新的问题/帖子,但是我如何使用localstorage
和save game
功能呢?