亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

JavaScript 比較 和 邏輯運(yùn)算符

比較和邏輯運(yùn)算子用於測試 true 或 false。


比較運(yùn)算子

#比較運(yùn)算子在邏輯語句中使用,以測定變數(shù)或值是否相等。

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

##描述範(fàn)例==等於x==8 為false=== 全等(值與型別)x===5 為true;x==="5" 為false!= 不等於x!=8 為true#>#大於#x>8為false<小於x<8 為true##>=#<=##小於或等於 x<=8 為true如何使用
#運(yùn)算子
大於或等於x>=8 為false
#可以在條件語句中使用比較運(yùn)算子對值進(jìn)行比較,然後根據(jù)結(jié)果來採取行動(dòng):

if (age<18) document.write("Too young");


我們將在下面的章節(jié)介紹更多有關(guān)條件語句的知識(shí)。


邏輯運(yùn)算子

#邏輯運(yùn)算子用於測定變數(shù)或值之間的邏輯。

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

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

條件運(yùn)算子

JavaScript 也包含了基於某些條件對變數(shù)進(jìn)行賦值的條件運(yùn)算子。

語法

variablename=(condition)?value1:value2?

實(shí)例

如果變數(shù)age 中的值小於18,則向變數(shù)voteable 賦值"年齡太小",否則賦值"年齡已達(dá)到"。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>php中文網(wǎng)(php.cn)</title>
</head>
<body>
<p>點(diǎn)擊按鈕檢測年齡。</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>

執(zhí)行程式嘗試



# 繼續(xù)學(xué)習(xí)
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>點(diǎn)擊按鈕檢測年齡。</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>
提交重置程式碼
<table id="ego8f"></table>