在javascript中如何根据用户指向的内容显示不同的文本?

时间:2016-01-06 12:45:13

标签: javascript jquery

当用户得分超过30分时我想显示:“你赢了”,当积分小于30时我想要显示:“你输了”。这是我的脚本代码的一部分:

update_time: function() {
    this.time -= 1;
    if (this.time > 0) {
      if (String(this.time).length == 1) {
        this.time = "0" + this.time;
      }
      $(".info .time").html("00:" + this.time);
    } else {
      $(".info .time").html("00:00");
      clearInterval(this.time_interval);
      $(".end .score").html("Game over!<br />You scored " + this.points + " points!").parent().fadeIn("fast");
    }
  },

  add_point: function() {
    this.set_dot();
    this.points += 1;
    $(".info .points").html(this.points);
  },

  set_dot: function() {
    $(this.dot_selector).removeClass("active");
    var active = Math.floor((Math.random() * ($(this.dot_selector).length - 1))+1);
    $(this.dot_selector + ":eq("+active+")").addClass("active");
  }
};

这是一些html:

div class='info'>
  <div class='time'></div>
  <div class='points'></div>
</div>
<div class='wall'>
.....
<div class='dot'></div>
  <div class='dot'></div>
  <div class='dot'></div>
  <div class='start'>
    <button>Start game!</button>
  </div>
  <div class='end'>
    <div class='score'></div>
    <button>Play again!</button>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

试试这段代码;

update_time: function() {
    this.time -= 1;
    if (this.time > 0) {
      if (String(this.time).length == 1) {
        this.time = "0" + this.time;
      }
      $(".info .time").html("00:" + this.time);
    } else {
      $(".info .time").html("00:00");
      clearInterval(this.time_interval);
      var pts = $(".points").html;
      alert(pts);
      if(pts<30){
          $(".end .score").html("Game over!<br />You scored " + this.points + " points!. You Lose").parent().fadeIn("fast");
      }else{
          $(".end .score").html("Game over!<br />You scored " + this.points + " points! You won").parent().fadeIn("fast");
      }
    }
},

希望这有帮助。