我正在学习javascript中的Object。在下面的代码中,每当我单击Spawn时,会立即生成一个新的播放器。我想要做的是,当我点击Spawn时,播放器不会立即生成,而是创建一个带有其名称的按钮。然后当我点击该按钮时,产生该按钮名称的玩家。
我知道如果我有播放器数据库,我可以很容易地做到这一点但是如果这只是暂时的,那么没有数据库。我该怎么做?
function spawnPlayer() {
var name_element = document.getElementById('name')
var race_element = document.getElementById('race')
var job_element = document.getElementById('job')
var player = new Player(name_element.value, race_element.value, job_element.value)
var msg = player.race+' '+player.job+' '+player.name+' enter the arena.'
var player_element = document.getElementById('player')
player_element.innerHTML += '<p>'+msg+'</p>'
name_element.value = ''
race_element.value = ''
job_element.value = ''
}
function Player(name, race, job) {
this.name = name
this.race = race
this.job = job
}
<input type="text" placeholder="name" id="name">
<br>
<input type="text" placeholder="race" id="race">
<br>
<input type="text" placeholder="job" id="job">
<br>
<input type="button" onclick="spawnPlayer()" value="Spawn">
<div id="player">
</div>