JavaScript 運(yùn)算符
運(yùn)算符 = 用于賦值。
運(yùn)算符 + 用于加值。
運(yùn)算符 = 用于給 JavaScript 變量賦值。
算術(shù)運(yùn)算符 + 用于把值加起來。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>點(diǎn)擊按鈕計(jì)算 x 的值.</p> <button onclick="myFunction()">點(diǎn)擊這里</button> <p id="demo"></p> <script> function myFunction() { y=5; z=2; x=y+z; document.getElementById("demo").innerHTML=x; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
JavaScript 算術(shù)運(yùn)算符
加法:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>假設(shè) y=5,計(jì)算 x=y+2,并顯示結(jié)果。</p> <button onclick="myFunction()">點(diǎn)擊這里</button> <p id="demo"></p> <script> function myFunction(){ var y=5; var x=y+2; var demoP=document.getElementById("demo") demoP.innerHTML="x=" + x; } </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>設(shè)置 y=5, 計(jì)算出 x=y-2, 并輸出結(jié)果。</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo"></p> <script> function myFunction() { var y=5; var x=y-2; var demoP=document.getElementById("demo"); demoP.innerHTML="x=" + x; } </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>設(shè)置 y=5, 計(jì)算出 x=y*2, 并輸出結(jié)果。</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo"></p> <script> function myFunction() { var y=5; var x=y*2; var demoP=document.getElementById("demo") demoP.innerHTML="x=" + x; } </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>設(shè)置 y=5, 計(jì)算 x=y/2, 并輸出結(jié)果。</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo"></p> <script> function myFunction() { var y=5; var x=y/2; var demoP=document.getElementById("demo") demoP.innerHTML="x=" + x; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
取模(余數(shù))
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>設(shè)置 y=5, 計(jì)算出 x=y%2, 并顯示結(jié)果。</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo"></p> <script> function myFunction() { var y=5; var x=y%2; var demoP=document.getElementById("demo") demoP.innerHTML="x=" + x; } </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>設(shè)置 y=5, 計(jì)算出 x=++y, 并顯示結(jié)果。</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo"></p> <script> function myFunction() { var y=5; var x=++y; var demoP=document.getElementById("demo") demoP.innerHTML="x=" + x + ", y=" + y; } </script> <p><strong>注意:</strong>兩個(gè)值 x 和 y 都受到影響。</p> </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>設(shè)置 y=5, 計(jì)算出 x=y++, 并顯示結(jié)果。</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo"></p> <script> function myFunction() { var y=5; var x=y++; var demoP=document.getElementById("demo") demoP.innerHTML="x=" + x + ", y=" + y; } </script> <p><strong>注意:</strong> x 和 y 兩個(gè)值都受到影響。</p> </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>設(shè)置 y=5, 計(jì)算出 x=--y, 并顯示結(jié)果。</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo"></p> <script> function myFunction() { var y=5; var x=--y; var demoP=document.getElementById("demo") demoP.innerHTML="x=" + x + ", y=" + y; } </script> <p><strong>注意:</strong> x 和 y 兩個(gè)值都會(huì)被影響。</p> </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>設(shè)置 y=5, 計(jì)算出 x=y--, 并顯示結(jié)果。</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo"></p> <script> function myFunction() { var y=5; var x=y--; var demoP=document.getElementById("demo") demoP.innerHTML="x=" + x + ", y=" + y; } </script> <p><strong>注意:</strong>x 和 y 兩個(gè)值都會(huì)受到影響。</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
JavaScript 賦值運(yùn)算符
賦值運(yùn)算符用于給 JavaScript 變量賦值。
給定 x=10 和 y=5,下面的表格解釋了賦值運(yùn)算符:
=:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>設(shè)置 x=10 和 y=5, 計(jì)算 x=y, 并顯示結(jié)果。</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo"></p> <script> function myFunction() { var x=10; var y=5; x=y; var demoP=document.getElementById("demo") demoP.innerHTML="x=" + x; } </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>設(shè)置 x=10 和 y=5, 計(jì)算 x+=y, 并顯示結(jié)果。</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo"></p> <script> function myFunction() { var x=10; var y=5; x+=y; var demoP=document.getElementById("demo") demoP.innerHTML="x=" + x; } </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>設(shè)置 x=10 和 y=5, 計(jì)算 x-=y, 并顯示結(jié)果。</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo"></p> <script> function myFunction() { var x=10; var y=5; x-=y; var demoP=document.getElementById("demo") demoP.innerHTML="x=" + x; } </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>設(shè)置 x=10 和 y=5, 計(jì)算 x*=y, 并顯示結(jié)果。</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo"></p> <script> function myFunction() { var x=10; var y=5; x*=y; var demoP=document.getElementById("demo") demoP.innerHTML="x=" + x; } </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>設(shè)置 x=10 和 y=5, 計(jì)算 x/=y, 并顯示結(jié)果。</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo"></p> <script> function myFunction() { var x=10; var y=5; x/=y; var demoP=document.getElementById("demo"); demoP.innerHTML="x=" + x; } </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>設(shè)置 x=10 和 y=5, 計(jì)算 x%=y, 并顯示結(jié)果。</p> <button onclick="myFunction()">點(diǎn)我</button> <p id="demo"></p> <script> function myFunction() { var x=10; var y=5; x%=y; var demoP=document.getElementById("demo") demoP.innerHTML="x=" + x; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
用于字符串的 + 運(yùn)算符
+ 運(yùn)算符用于把文本值或字符串變量加起來(連接起來)。
如需把兩個(gè)或多個(gè)字符串變量連接起來,請(qǐng)使用 + 運(yùn)算符。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>點(diǎn)擊按鈕創(chuàng)建及增加字符串變量。</p> <button onclick="myFunction()">點(diǎn)擊這里</button> <p id="demo"></p> <script> function myFunction() { txt1="What a very"; txt2="nice day"; txt3=txt1+txt2; document.getElementById("demo").innerHTML=txt3; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
要想在兩個(gè)字符串之間增加空格,需要把空格插入一個(gè)字符串之中:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>點(diǎn)擊按鈕創(chuàng)建及增加字符串變量。</p> <button onclick="myFunction()">點(diǎn)擊這里</button> <p id="demo"></p> <script> function myFunction() { txt1="What a very "; txt2="nice day"; txt3=txt1+txt2; document.getElementById("demo").innerHTML=txt3; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
或者把空格插入表達(dá)式中:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>點(diǎn)擊按鈕創(chuàng)建及增加字符串變量。</p> <button onclick="myFunction()">點(diǎn)擊這里</button> <p id="demo"></p> <script> function myFunction() { txt1="What a very"; txt2="nice day"; txt3=txt1+" "+txt2; document.getElementById("demo").innerHTML=txt3; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
對(duì)字符串和數(shù)字進(jìn)行加法運(yùn)算
兩個(gè)數(shù)字相加,返回?cái)?shù)字相加的和,如果數(shù)字與字符串相加,返回字符串,如下實(shí)例:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>點(diǎn)擊按鈕創(chuàng)建及增加字符串變量。</p> <button onclick="myFunction()">點(diǎn)擊這里</button> <p id="demo"></p> <script> function myFunction() { var x=5+5; var y="5"+5; var z="Hello"+5; var demoP=document.getElementById("demo"); demoP.innerHTML=x + "<br>" + y + "<br>" + z; } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
規(guī)則:如果把數(shù)字與字符串相加,結(jié)果將成為字符串!