[英]Changing where to put text
I have a very basic if statement and it reads like this: 我有一个非常基本的if语句,它的内容如下:
var p1 = document.getElementById('p1').value;
var p2 = document.getElementById('p2').value;
if(p1==p2){
document.write("P1=P2");
} else {
document.write("P1 Does Not Equal P2")
};
Followed by the following HTML 随后是以下HTML
<p id="p1">2</p>
<p id="p2">4</p>
I just want to be able to identify the if the value within p1
is greater or less than the value of p2
. 我只希望能够确定p1
的值是大于还是小于p2
的值。 Eventually this will have bigger consequences than just document.write
but at this time the if statement does not recognize the content in between the <p></p>
. 最终,这将比document.write
产生更大的后果,但此时if语句无法识别<p></p>
之间的内容。
Paragraph tags don't have a value, so you would need to use innerHTML. 段落标签没有值,因此您需要使用innerHTML。
var p1 = document.getElementById('p1').innerHTML;
var p2 = document.getElementById('p2').innerHTML;
if(p1==p2){
console.log("P1=P2");
} else {
console.log("P1 Does Not Equal P2")
};
However, that doesn't solve the 2nd part of the problem, since you mention that eventually you will need to know > or <, also. 但是,这不能解决问题的第二部分,因为您提到最终您还需要知道>或<。 To make that happen, you would can change your tags to something else (a div?) or convert the resulting values to numbers. 为此,您可以将标签更改为其他标签(div?),也可以将结果值转换为数字。
var p1 = document.getElementById('p1').innerHTML;
var p2 = document.getElementById('p2').innerHTML;
var v1 = Number(p1);
var v2 = Number(p2)
console.log(p1);
console.log(p2);
console.log(v1);
console.log(v2);
if(v1==v2){
console.log("P1=P2");
} else {
console.log("P1 Does Not Equal P2")
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.