【问题标题】:Storing prompt as object value将提示存储为对象值
【发布时间】:2016-07-03 05:47:24
【问题描述】:

我正在制作一个游戏,当您单击按钮时会与怪物战斗,并且伤害等式基于玩家和怪物的统计数据。我得到了让怪物的对象在方程式中工作的帮助,但我不知道如何让 Player 对象正常工作,因为它没有被存储为选择选项。 这是我之前发的帖子,你先看这里的答案会更容易理解我的代码Other Post

Here is my current fiddle

我需要能够提示用户输入他们的用户名,但仍然在等式中使用他们的统计信息。

function Player(username, lvl, exp, gold, hp, atk, def, spd) {
    var self = this;
    this.username = username;
    this.lvl = lvl;
    this.exp = exp;
    this.gold = gold;
    this.hp = hp;
    this.atk = atk;
    this.def = def;
    this.spd = spd;
    this.implement = function() {
        var h1 = document.getElementById('user');
        h1.innerText = this.username;
        h1.addClass('playerName');
        $(h1).data('player', self)
    }
    this.implement();
}
var newPlayer = new Player(prompt("What is your username?"), 1, 0, 0, 10, 2, 2, 2);
playerEl = $('.playerName');
player = playerEl.data('player');

【问题讨论】:

  • h1.addClass('playerName'); 不起作用,因为没有 addClass elemet 方法
  • 真正的要求是什么..应该发生什么?输入的名称显示在user 元素中
  • 但我有变量 h1。
  • jsfiddle.net/5cdwu23d/3 这就是战斗按钮和选择菜单应该做的事情
  • addClass 方法用于 jQuery 对象,你所拥有的是一个 dom 元素引用...$('#user').text(this.username).addClass('playerName').data('player', self)

标签: javascript jquery object dom


【解决方案1】:

添加playerName类时出错,h1是一个没有addClass方法的dom元素引用。

既然你有 jQuery,你可以使用

  this.implement = function() {
    $('#user').text(this.username).addClass('playerName').data('player', this)
  }

【讨论】:

    猜你喜欢
    • 2016-04-29
    • 1970-01-01
    • 2011-07-14
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 2012-12-30
    相关资源
    最近更新 更多