如何设置" p"将数据值标记为数字?

时间:2017-02-06 14:41:42

标签: javascript html

当您第一次学习编码时,您会了解到有不同的价值类型。字符串,如true,布尔值,如1,以及数字,如<p>。如何将def curry[A,B,C](f: (A, B) => C): A => (B => C) = a => b => f(a, b) 标记的值设置为数字而不是字符串?或者是否有单独的数字标签?

5 个答案:

答案 0 :(得分:1)

Javascript是一种无类型语言。变量类型是从该变量的值推导出来的。变量的类型也可以更改为其分配的另一种类型的值。所以这样的事情(例如C语言):

int a; // a is an int (it will be always an int)
float f = 5.34; // f is a float (it will always be a float)

在Javascript中不存在。 Javascript可以使用相同的变量来存储多个类型,而无需重新声明变量。

var a = 45; // a type is deduced from the current assignement so for now it's a number
a = "string"; // now the type of a is a string not a number anymore 

您可以明确隐式在需要时将一种类型转换为另一种类型。

明确的对话:

您可以使用.toString将数字转换为字符串(尽管没有必要):

var num = 456;

console.log("num is of type: " + typeof(num));

var str = num.toString(); //explicitly change num to string

console.log("str is of type: " + typeof(str));

如果字符串是整数,您还可以使用parseInt显式地将字符串转换为数字(使用很多),如果字符串是a,则parseFloat浮动,或Number得到这样的:

var str = '123.45.99';

console.log("str is of type: " + typeof(str));

var num1 = parseInt(str); // will parse an integer untill the first non integer character is found (will return 12 if str == "12ppp")

console.log("num1 is of type: " + typeof(num1));

var num2 = parseFloat(str); // parses a float untill the first non float character is found (will return 75.56 if str == "75.56.55abc"

console.log("num2 is of type: " + typeof(num2));

var num3 = Number(str); // get number representation of the string (will fail (return NaN) if the string is not a valid number like "123r33")

console.log("num3 is of type: " + typeof(num3));

隐含对话:

隐含对话就是当你让译员除了处理你的类型变量之外别无选择。从而防止它错误地解释它们。你可以通过很多方式实现这一目标。

要隐含地将数字转换为字符串,只需将空字符串添加到该数字中,如下所示:

var num = 345;

console.log("num is of type: " + typeof(num));

var str = "" + num;

console.log("str is of type: " + typeof(str));

要将字符串转换为数字,有多种方式:

var str = '123.45';

console.log("str is of type: " + typeof(str));

var num1 = +str; // a unary operator (that is valid for numbers only) forces the interpretter to use the value of str as a number

console.log("num1 is of type: " + typeof(num1));

由于+运算符(不是unray one)可用于连接和添加,如果其中一个操作数是字符串,则解释器将始终支持连接的连接。但是另一个运算符(/*/%)仅用于数字,因此当字符串除以另一个数字时,解释器将被强制使用将字符串的值用作数字:

var str = "10";

var num1 = str + 5;

console.log("num1 is: " + num1 + " and it's of type: " + typeof(num1)); // print out the wrong expectation, because since one of the operands of the + is a string (str) the interpretter will think it's a concatenation


var num2 = str * 5; // since * could be used with just numbers, the interpretter is forced to use str as a number (implicitly converting str to a number)

console.log("num2 is: " + num2 + " and it's of type: " + typeof(num2));


// to make the + operator work you'll have to use the unary operator to implicitly convert str before adding the numbers

var num3 = +str + 5;

console.log("num3 is: " + num3 + " and it's of type: " + typeof(num3));

// ++ and -- are also used with just number ..

str++; // since str++ is str = something and that something is calculated as number str will implicitly take the value of a number thus when used again it will be deduced as a number


console.log("str is: " + str + " and it's of type: " + typeof(str)); // in the previous assignment, str took the value of a number thus becoming a number

答案 1 :(得分:0)

HTML <p>标记专门代表段落。因此它应该将其中的所有内容视为文本字符串。

如果您确定它包含数值,则可以使用parseFloat(yourPtext.replace(',',''));将其转换为数字,并在javascript代码中使用它。

答案 2 :(得分:0)

<p>是用于包装段落的元素。 <p>标记的值始终为文本。检查W3C HTML5 standard,您发现<p>元素的内容模型Phrasing content

  

短语内容是文档的文本,以及在段落内标记该文本的元素。短语内容的运行形成段落。

因此,您可以在<p>元素中显示数字,但它们始终表示为字符串。您可以将段落的值作为字符串获取,然后将其解析为数字:

&#13;
&#13;
var par = document.getElementById('p1');

var val = par.innerHTML;
console.log('val:', typeof val); // Return "string"

var valNumber = parseInt(val); // Parsing the string value to integer
console.log('valNumber:', typeof valNumber); // Return "number"
&#13;
<p id="p1">123</p>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

假设您使用<p>参数作为文本段落。

尝试将<p></p>包裹在<div>

旁边

然后给它一个id。

像这样:

<div id="myClass"><p></p></div>

然后添加一些像这样的javascript:

<script>
var element = document.getElementById("myClass");
element.innerText = "1";
</script>

可能想看一下这个链接

http://www.w3schools.com/js/js_htmldom_html.asp

答案 4 :(得分:0)

使用jquery可以通过两种方式实现

1) var pText = $("p").text()
var number = Number(pText)
2) var pText = $("p").text()
var number = parseInt(pText,10)

或将<p>标记替换为<input type="number" value="" >