如何使用jQuery在div内获取数字

问题描述:

如何获取div中的数字并将其用作JavaScript/jQuery中的number?这是我所拥有的:

How do I get a number inside a div and use it as a number in JavaScript/jQuery? Here is what I have:

HTML:

<div class="number">25</div>

jQuery:

var numb = $('.number').val();
console.log(numb);

jsfiddle: http://jsfiddle.net/nw9rLfba/

jsfiddle: http://jsfiddle.net/nw9rLfba/

.val()用于获取诸如inputselect等表单元素的值.您不能在div s上使用它.您可以使用.text()来获取div元素的文本内容.

.val() is for getting the value of form elements like input, select, etc. You can't use it on divs. You can use .text() to get the text content of the div element.

var number = $('.number').text();

但是它将以字符串形式返回内容.要将其转换为Javascript数字,必须使用parseInt():

But it will return the content as a string. To convert it to a Javascript number, you have to use parseInt():

var number = parseInt($('.number').text());