ÍøÂçѧԺ

w3pop.com :: ÍøÂçѧԺ :: JavaScript :: JS ²Ù×÷·û

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

JavaScript
JSÊýѧ¶ÔÏó²Î¿¼
JS×Ö·û´®¶ÔÏó²Î¿¼
JSº¯Êý²Î¿¼
JSʼþ²Î¿¼
Javascript ³£ÓÃÕ..
FFºÍIEϵÄjs¼æÈÝ..
jQuery ¼òµ¥½éÉÜ
jQuery / ºËÐÄ / ..
jQuery / ºËÐÄ / ..
ÈçºÎʹÓÃJSÀ´ÅжÏ..
JavascriptÔÚIEºÍ..
3¸öjs×Ö·û±àÂ뺯Ê..
javascript ÖÐµÄ ..

JS ²Ù×÷·û


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ä¯ÀÀ:15662 :: ::

Arithmetic Operators
ËãÊõÔËËã·û

Operator Description Example Result
+ Addition
¼Ó
x=2
y=2
x+y
4
- Subtraction
¼õ
x=5
y=2
x-y
3
* Multiplication
³Ë
x=5
y=4
x*y
20
/ Division
³ý
15/5
5/2
3
2.5
% Modulus (division remainder)
ÓàÊý
5%2
10%8
10%2
1
2
0
++ Increment
µÝÔö
x=5
x++
x=6
-- Decrement
µÝ¼õ
x=5
x--
x=4

Assignment Operators
¸³ÖµÔËËã·û

Operator Example Is The Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y x=x%y

Comparison Operators
±È½Ï£¨¹ØÏµ£©ÔËËã·û

Operator Description Example
== is equal to
µÈÓÚ
5==8 returns false
=== is equal to (checks for both value and type)
µÈÓÚ£¨¼ì²éÖµºÍÀàÐÍ£©*È«ÎǺϲÅËãÏàµÈ
x=5
y="5"

x==y returns true
x===y returns false

!= is not equal
²»µÈÓÚ
5!=8 returns true
> is greater than
´óÓÚ
5>8 returns false
< is less than
СÓÚ
5<8 returns true
>= is greater than or equal to
´óÓÚµÈÓÚ
5>=8 returns false
<= is less than or equal to
СÓÚµÈÓÚ
5<=8 returns true

Logical Operators
Âß¼­ÔËËã·û

Operator Description Example
&& and
Óë
x=6
y=3

(x < 10 && y > 1) returns true

|| or
»ò
x=6
y=3

(x==5 || y==5) returns false

! not
·Ç
x=6
y=3

!(x==y) returns true

String Operator
´®·û£¨Á¬½Ó×÷Óã©

A string is most often text, for example "Hello World!". To stick two or more string variables together, use the + operator.
ÔÚÎÄ×Öµ±ÖÐʹÓõıȽ϶࣬¾ÙÀýÀ´Ëµ“Hello World!”Òª½«Á½¸ö»ò¶à¸ö×Ö·û´®±äÁ¿ÏνÓÔÚÒ»ÆðµÄ»°¾ÍµÃʹÓà + ·ûºÅ

txt1="What a very"
txt2="nice day!"

txt3=txt1+txt2 

The variable txt3 now contains "What a verynice day!".
txt3±äÁ¿ÏÖÔÚ°üº¬“What a verynice day!”£¨°Ñ1ºÍ2ÏÎ½ÓÆðÀ´ÁË£©

To add a space between two string variables, insert a space into the expression, OR in one of the strings.
Òª¸øÁ½¸ö×Ö·û´®±äÁ¿ÖмäÌí¼Ó¿Õ¸ñ¾ÍµÃÔÚ±í´ïʽÀï²åÈë¿Õ¸ñ£¬»òÔÚÆäÖеÄÒ»¸ö¼ÓÉÏ£¨¿Õ¸ñ£©

txt1="What a very"
txt2="nice day!"
txt3=txt1+" "+txt2
or
txt1="What a very "
txt2="nice day!"
txt3=txt1+txt2

The variable txt3 now contains "What a very nice day!".
ÏÖÔÚ±äÁ¿txt3Ϊ“What a very nice day!”

Conditional Operator
Ìõ¼þÔËËã·û

JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.
JSÓиù¾ÝÌõ¼þ²»Í¬¸ø±äÁ¿²»Í¬ÖµµÄÌõ¼þÔËËã·û

Syntax
Óï·¨

variablename=(condition)?value1:value2 

Example
Àý×Ó

greeting=(visitor=="PRES")?"Dear President ":"Dear "

If the variable visitor is equal to PRES, then put the string "Dear President " in the variable named greeting. If the variable visitor is not equal to PRES, then put the string "Dear " into the variable named greeting.
Èç¹û±äÁ¿visitorµÄÖµµÈÓÚPRESÄÇôgreetingµÄÖµ¾ÍΪ"Dear President "¡£Èç¹û²»ÎªPRESÄÇôgreetingµÄÖµ¾ÍΪ"Dear"

ÆÀÂÛ (5) 1 All