国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

JavaScript中文參考手冊(cè) / JavaScript 比較

JavaScript 比較


比較和邏輯運(yùn)算符用于測(cè)試  true 或者false


比較運(yùn)算符

比較運(yùn)算符在邏輯語(yǔ)句中使用,以測(cè)定變量或值是否相等。

x=5,下面的表格解釋了比較運(yùn)算符:

==:

實(shí)例

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x==8 的比較值結(jié)果。</p>
<button onclick="myFunction()">嘗試一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x==8;
}
</script>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

實(shí)例

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x==5 的比較值結(jié)果。</p>
<button onclick="myFunction()">嘗試一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x==5;
}
</script>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

===:

實(shí)例

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x==="5" 的比較值結(jié)果。</p>
<button onclick="myFunction()">嘗試一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x==="5";
}
</script>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

實(shí)例

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x===5 的比較值結(jié)果。</p>
<button onclick="myFunction()">嘗試一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x===5;
}
</script>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

!=:

實(shí)例

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x!=8 的比較值結(jié)果。</p>
<button onclick="myFunction()">嘗試一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x!=8;
}
</script>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

!==:

實(shí)例

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x!=="5" 的比較值結(jié)果。</p>
<button onclick="myFunction()">嘗試一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x!=="5";
}
</script>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

實(shí)例

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x!==5 的比較值結(jié)果。</p>
<button onclick="myFunction()">嘗試一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x!==5;
}
</script>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

>:

實(shí)例

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x>8 的比較值結(jié)果。</p>
<button onclick="myFunction()">嘗試一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x>8;
}
</script>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

<:

實(shí)例

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
</head>
<body>

<p>x=5, 返回  x<8 的比較值結(jié)果。</p>
<button onclick="myFunction()">嘗試一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x<8;
}
</script>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

>=:

實(shí)例

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x>=8 的比較值結(jié)果。</p>
<button onclick="myFunction()">嘗試一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x>=8;
}
</script>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

<=:

實(shí)例

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x<=8 的比較值結(jié)果。</p>
<button onclick="myFunction()">嘗試一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x<=8;
}
</script>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


如何使用

可以在條件語(yǔ)句中使用比較運(yùn)算符對(duì)值進(jìn)行比較,然后根據(jù)結(jié)果來(lái)采取行動(dòng):

if (age<18) x="Too young";

您將在本教程的下一節(jié)中學(xué)習(xí)更多有關(guān)條件語(yǔ)句的知識(shí)。


邏輯運(yùn)算符

邏輯運(yùn)算符用于測(cè)定變量或值之間的邏輯。

給定 x=6 以及 y=3,下表解釋了邏輯運(yùn)算符:

運(yùn)算符描述例子
&&and(x < 10 && y > 1) 為 true
||or(x==5 || y==5) 為 false
!not!(x==y) 為 true



條件運(yùn)算符

JavaScript 還包含了基于某些條件對(duì)變量進(jìn)行賦值的條件運(yùn)算符。

語(yǔ)法

variablename=(condition)?value1:value2 

例子


實(shí)例

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</title> 
</head>
<body>

<p>點(diǎn)擊按鈕檢測(cè)年齡。</p>
年齡:<input id="age" value="18" />
<p>是否達(dá)到投票年齡?</p>
<button onclick="myFunction()">點(diǎn)擊按鈕</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var age,voteable;
	age=document.getElementById("age").value;
	voteable=(age<18)?"年齡太小":"年齡已達(dá)到";
	document.getElementById("demo").innerHTML=voteable;
}
</script>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例