开发者

How to increase div content value?

开发者 https://www.devze.com 2023-03-01 11:35 出处:网络
I want to increase div value +1 in certain events.How can i get div value and increas开发者_开发问答e it?this.innerHTML = +this.innerHTML + 1;

I want to increase div value +1 in certain events.How can i get div value and increas开发者_开发问答e it?


this.innerHTML = +this.innerHTML + 1;

DEMO


Given:

<div id="x">42</div>

You can do this:

var $x = $('#x');
$x.text(parseInt($x.text(), 10) + 1);

For your education:

  • .text()
  • parseInt() (and always include the radix argument)


If the div's HTML is simply a number:

var element = document.getElementById('yourElementID');
element.innerHTML = parseInt(element.innerHTML, 10)+1;


Here you use below code

In javascript
--------------
var demoInt = $('#divInt').text();
demoInt =parseInt(demoInt);

In HTML
-------
<div id="divInt">111</div>
0

精彩评论

暂无评论...
验证码 换一张
取 消