Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用不同的按钮组合更改HTML代码_Javascript_Html_Arrays_Button_Combinations - Fatal编程技术网

Javascript 如何使用不同的按钮组合更改HTML代码

Javascript 如何使用不同的按钮组合更改HTML代码,javascript,html,arrays,button,combinations,Javascript,Html,Arrays,Button,Combinations,我写了一些代码,包括三个按钮和一个段落。 段落上写着“你好,我的名字是多莉。”按钮让你可以选择对多莉做三件事中的一件。你可以打招呼、拥抱或杀死他们,这些按钮分别给出响应。但是,是否可以为不同的响应按下按钮组合?例如,如果我按下按钮杀死多莉,然后按下按钮拥抱多莉,我能让它说一些关于拥抱尸体的话吗?如果是,怎么做?当然可以。在Javascript中为dolly创建变量/数组。每次执行推拉操作时,将其添加到此数组中。每次都要阅读数组并决定您的响应是什么。当然可以。在Javascript中为dolly创

我写了一些代码,包括三个按钮和一个段落。
段落上写着“你好,我的名字是多莉。”按钮让你可以选择对多莉做三件事中的一件。你可以打招呼、拥抱或杀死他们,这些按钮分别给出响应。但是,是否可以为不同的响应按下按钮组合?例如,如果我按下按钮杀死多莉,然后按下按钮拥抱多莉,我能让它说一些关于拥抱尸体的话吗?如果是,怎么做?

当然可以。在Javascript中为dolly创建变量/数组。每次执行推拉操作时,将其添加到此数组中。每次都要阅读数组并决定您的响应是什么。

当然可以。在Javascript中为dolly创建变量/数组。每次执行推拉操作时,将其添加到此数组中。每次读取数组并决定您的响应应该是什么。

这可以通过为dolly保留一个状态对象来实现

var dolly = {
  is_alive: true,
  hugCount: 0,
  helloCount: 0,
  message: function() {
    if(!this.is_alive) {
      //whatever you want to print if dolly's dead.
    } 
    if(this.hugCount) {
      //whatever for a certain number of hug counts.
    }        
    if(this.helloCount) {
      //whatever for a certain number of hello counts.
    }

  },
  kill: function(){
    if(this.is_alive){
      this.is_alive = false;
      return this.message();
    }
  }
};
如果这是一个模拟游戏原型,您可以继续添加更多功能。只需向对象添加更多函数,如果需要添加更多像tina或james这样的人,也可以创建构造函数

var Person = function() {
  this.is_alive = true,
  this.hugCount = 0,
  this.helloCount = 0,
};
Person.prototype.message = function() {
    if(!this.is_alive) {
      //whatever you want to print if dolly's dead.
    } 
    if(this.hugCount) {
      //whatever for a certain number of hug counts.
    }        
    if(this.helloCount) {
      //whatever for a certain number of hello counts.
    }

};
Person.prototype.kill = function(){
    if(this.is_alive){
      this.is_alive = false;
      return this.message();
    }
};
Person.prototype.hello = function() {
  this.helloCount+= 1;
  return this.message();
}
现在,您可以使用相同的功能生成任意数量的娃娃

var dolly = new Person();
dolly.kill(); //YOU DIED!
编辑1

根据Norman Bentley的建议,您还可以使用数组跟踪用户与“dolly”的交互

编辑2

要将其与HTML一起嵌入,请参考此JS FIDLE


这可以通过为推拉保留状态对象来实现

var dolly = {
  is_alive: true,
  hugCount: 0,
  helloCount: 0,
  message: function() {
    if(!this.is_alive) {
      //whatever you want to print if dolly's dead.
    } 
    if(this.hugCount) {
      //whatever for a certain number of hug counts.
    }        
    if(this.helloCount) {
      //whatever for a certain number of hello counts.
    }

  },
  kill: function(){
    if(this.is_alive){
      this.is_alive = false;
      return this.message();
    }
  }
};
如果这是一个模拟游戏原型,您可以继续添加更多功能。只需向对象添加更多函数,如果需要添加更多像tina或james这样的人,也可以创建构造函数

var Person = function() {
  this.is_alive = true,
  this.hugCount = 0,
  this.helloCount = 0,
};
Person.prototype.message = function() {
    if(!this.is_alive) {
      //whatever you want to print if dolly's dead.
    } 
    if(this.hugCount) {
      //whatever for a certain number of hug counts.
    }        
    if(this.helloCount) {
      //whatever for a certain number of hello counts.
    }

};
Person.prototype.kill = function(){
    if(this.is_alive){
      this.is_alive = false;
      return this.message();
    }
};
Person.prototype.hello = function() {
  this.helloCount+= 1;
  return this.message();
}
现在,您可以使用相同的功能生成任意数量的娃娃

var dolly = new Person();
dolly.kill(); //YOU DIED!
编辑1

根据Norman Bentley的建议,您还可以使用数组跟踪用户与“dolly”的交互

编辑2

要将其与HTML一起嵌入,请参考此JS FIDLE



为多利的状态保留一个变量。请显示您的代码,这将有助于处理您的状态question@Chewynugget你从答案中受益了吗?@AmreshVenugopal是的,我受益了!一般来说,关于代码的问题应该包括您尝试过的代码以及您在解决方案中遇到的困难。通过为dolly的状态保留一个变量。请显示您的代码,这将有助于处理您的question@Chewynugget你从答案中受益了吗?@AmreshVenugopal是的,我受益了!一般来说,关于代码的问题应该包括您尝试过的代码以及您在解决方案中遇到的困难。好帖子!如果事件顺序无关紧要,则使用如上计数。如果是这样,您可能需要使用数组。@NormanBentley谢谢,没错!我将为此添加一个编辑。非常感谢@AmreshVenugopalSorry,我对编码非常陌生。如何将此脚本附加到HTML按钮?我将稍后添加它@祝你好运!如果事件顺序无关紧要,则使用如上计数。如果是这样,您可能需要使用数组。@NormanBentley谢谢,没错!我将为此添加一个编辑。非常感谢@AmreshVenugopalSorry,我对编码非常陌生。如何将此脚本附加到HTML按钮?我将稍后添加它@丘伊努格特