解析 Int 减法在 JS(Ionic 5 + Angular)中如何工作?

How does parse Int subtraction work in JS (Ionic 5 + Angular)?

我正在使用 Angular 在 Ionic 5 中开发基于点的问题游戏。我之前问过如何使加法按钮按预期工作,效果很好,但现在当尝试使用相同的方法进行点减法时,我得到一个 "Nan" 框。

我已经尝试重新设计我的 GUI 以查看问题是否出在这方面,但没有任何改变。

我是 Ionic 的超级初学者,HTML 总的来说,所以我的代码可能会很垃圾。任何帮助都会很棒!

下面是我修改之前的代码。


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Point Game</title>
  <script type="module" src="https://cdn.jsdelivr.net/npm/@ionic/core@4.7.4/dist/ionic/ionic.esm.js"></script>
  <script nomodule src="https://cdn.jsdelivr.net/npm/@ionic/core@4.7.4/dist/ionic/ionic.js"></script>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ionic/core@4.7.4/css/ionic.bundle.css"/>
  <style>
    :root {
      --ion-safe-area-top: 20px;
      --ion-safe-area-bottom: 22px;
    }
  </style>
  <script>

    function openMenu() {
      document.querySelector('ion-menu-controller').open();
    }

    function p1point(){
      var p1pp = document.getElementById("p1p").innerText; //getting text inside element
      p1pp = parseInt(p1pp) + 1; //Increment
      document.getElementById("p1p").innerHTML = p1pp; // Writing values
    }
    function p2point(){
      var p2pp = document.getElementById("p2p").innerText; //getting text inside element
      p2pp = parseInt(p2pp) + 1; //Increment
      document.getElementById("p2p").innerHTML = p2pp; // Writing values
    }
    function p1minus(){
      var p1mm = document.getElementById("p1min").innerText; //getting text inside element
      p1mm = parseInt(p1mm) - 1; //Increment
      document.getElementById("p1min").innerHTML = p1mm; // Writing values
    }
    function p2minus(){
      var p2mm = document.getElementById("p2min").innerText; //getting text inside element
      p2mm = parseInt(p2mm) - 1; //Increment
      document.getElementById("p2min").innerHTML = p2mm; // Writing values
    }
  </script>
</head>
<body>
  <ion-app>
    <ion-menu side="start">
      <ion-header>
        <ion-toolbar translucent>
          <ion-title>Menu</ion-title>
        </ion-toolbar>
      </ion-header>
      <ion-content>
        <ion-list>
          <ion-item>
            <ion-icon name="home" slot="start"></ion-icon>
            <ion-label>Home</ion-label>
          </ion-item>
          <ion-item>
            <ion-icon name="person" slot="start"></ion-icon>
            <ion-label>Profile</ion-label>
          </ion-item>
          <ion-item>
            <ion-icon name="chatbubbles" slot="start"></ion-icon>
            <ion-label>Messages</ion-label>
          </ion-item>
          <ion-item>
            <ion-icon name="settings" slot="start"></ion-icon>
            <ion-label>Settings</ion-label>
          </ion-item>
        </ion-list>
      </ion-content>
    </ion-menu>

    <div class="ion-page" main>
      <ion-header>
        <ion-toolbar>
          <ion-buttons slot="start">
            <ion-menu-button></ion-menu-button>
          </ion-buttons>
          <ion-title>Point Game</ion-title>
        </ion-toolbar>
      </ion-header>
      <ion-content class="ion-padding">
        <ion-grid>
          <ion-row>
            <ion-col>
              <ion-card>
                <!--Player 1 score-->
                <ion-card-header>
                  <ion-card-subtitle class = "ion-text-center">Player 1</ion-card-subtitle>
                  <ion-card-title class = "ion-text-center" id = "p1p">0</ion-card-title>
                </ion-card-header>
              </ion-card>
            </ion-col>
            <ion-col size = -1>
              <!-- fake inner thing psuedo-col-->

            </ion-col>
            <ion-col>
              <ion-card>
                <!--Player 2 score-->
                <ion-card-header>
                  <ion-card-subtitle class = "ion-text-center">Player 2</ion-card-subtitle>
                  <ion-card-title class = "ion-text-center" id = "p2p">0</ion-card-title>
                </ion-card-header>
              </ion-card>
            </ion-col>
          </ion-row>
          <ion-row>
            <ion-col width-50 style = "text-align: right">
              <ion-button id = "p1plus" onclick="p1point()" > + </ion-button>
            </ion-col>
            <ion-col size = .15>
              <!-- fake inner thing psuedo-col-->

            </ion-col>
            <ion-col width-50 style = "text-align: right">
              <ion-button id = "p2plus" onclick="p2point()"> + </ion-button>
            </ion-col>
            <ion-col size = -3>
              <!-- fake inner thing psuedo-col-->

            </ion-col>
          </ion-row>
        </ion-row>
        <ion-row>
          <ion-col width-50 style = "text-align: right">
            <ion-button id = "p1min" onclick="p1minus()" >  -  </ion-button>
          </ion-col>
          <ion-col size = .15>
            <!-- fake inner thing psuedo-col-->

          </ion-col>
          <ion-col width-50 style = "text-align: right">
            <ion-button id = "p2min" onclick="p2minus()">  -  </ion-button>
          </ion-col>
          <ion-col size = -3>
            <!-- fake inner thing psuedo-col-->

          </ion-col>
        </ion-row>
        </ion-grid>

      </ion-content>
    </div>
  </ion-app>
  <ion-menu-controller></ion-menu-controller>


</body>
</html>

我认为你的减号函数的问题是你得到了按钮的 innerText。您正在使用 p1minp2min id,这些文本不是数字。这就是为什么你得到 NaN.