如何使我的程序重新运行特定的代码行?

时间:2019-03-16 03:38:29

标签: javascript html

我正在制作游戏,并且我有js提示人员分配他们的所有技能点。当他们受到伤害或治愈时,他们需要按一下按钮才能更改其健康状态值。问题是,当他们输入新值时,用于编辑统计信息和滚动d20的所有按钮都会消失

作为参考,在程序开始时,玩家确定所有状态后,将显示其所有状态及其值,并滚动显示每个状态并编辑其​​hp值的按钮。

这大概就是我所拥有的

<script src="javascript.js">
</script>

<script>
    function hpChange() {
        var health = prompt('What is your new HP value?');
        document.write(prints all of the stats with their values);
    }
</script>

<form>
    <input type="button" onclick="hpChange()" value="Change HP"/>
</form>

(这是我自己的DnD版本,我与具有自定义规则的朋友一起玩)

before

after

1 个答案:

答案 0 :(得分:2)

我认为最好通过其元素来操纵文档。 例如,您可以将统计信息表示为HTML代码中的项目列表:

index.html:

UIImageView

还有一个外部JS文件,该文件将更改统计信息的值:

javascript.js:

<html>
  <body>
    <ul>
    <!-- Here we assign a unique ID to the element to get it later in our JS file -->
      <li id="hp">HP: 10</li>
      <li>STR: 5</li>
      <li>AGI: 5</li>
      <li>INT: 5</li>
      <li>CHA: 5</li>
      <li>PER: 5</li>
      <li>LUC: 5</li>    
    </ul>
     <!-- Here we assign a unique ID to the button to get it later in our JS file -->
   <button id="rollForHealth">
   Roll for Health
   </button>
   <!-- It's better to link an outer JS file at the bottom of the page to initialize it when the content is loaded -->
  <script src="javascript.js"></script>
  </body>
</html>

这就是它在jsfiddle上的工作方式

您可以对其他统计信息进行同样的操作。

我希望它能对您有所帮助,并且您会喜欢DnD中的会议。

相关问题