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

JavaScript 比較


比較和邏輯運算符用于測試  true 或者false。


比較運算符

比較運算符在邏輯語句中使用,以測定變量或值是否相等。

x=5,下面的表格解釋了比較運算符:

==:

實例

<!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>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例

實例

<!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>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例

===:

實例

<!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>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例

實例

<!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>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例

!=:

實例

<!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>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例

!==:

實例

<!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>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例

實例

<!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>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例

>:

實例

<!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>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例

<:

實例

<!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>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例

>=:

實例

<!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>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例

<=:

實例

<!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>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例


如何使用

可以在條件語句中使用比較運算符對值進行比較,然后根據(jù)結(jié)果來采取行動:

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

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


邏輯運算符

邏輯運算符用于測定變量或值之間的邏輯。

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

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



條件運算符

JavaScript 還包含了基于某些條件對變量進行賦值的條件運算符。

語法

variablename=(condition)?value1:value2 

例子


實例

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

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

</body>
</html>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例