如何通过向元素添加整数值来更改innerHtml? [英] how to change innerHtml by adding a integer value to element?

查看:109
本文介绍了如何通过向元素添加整数值来更改innerHtml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个记分板

当有人单击+500按钮时,它将在计分板上的值增加500,即在p标签的值增加500.

what it does when someone click the +500 button it will add 500 to the value in the score board i.e it will add 500 to the value of p tag

   <div class="box"> 
 <h2>Teams</h2>
 <div class="score">
    <p id="p1" class="lead">230</p>
  </div>
  /div>
 <button id="b1">+500</button>

使用JavaScript

JavaScript for it

var myScore = document.getElementById("b1");
myScore.onclick = function () {
    var newScore = document.getElementById("p1").innerHTML;
    var value = newScore + 500;
    document.getElementById("p1").innerHTML = value;
};

但这显示的是我 230500 而不是 730 .如何以整数形式更改我的内部html值230?

but this is showing me 230500 instead of 730. how to change my inner html value 230 in integer form ??

推荐答案

现在,您要在字符串中添加一个整数,因此要进行字符串连接.

Right now, you're adding an integer to a string, so you're making a string concatenation.

更改

 var value = newScore + 500;

 var value = parseInt(newScore,10) + 500;

这篇关于如何通过向元素添加整数值来更改innerHtml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆