找回密码
 注册
搜索
热搜: 回贴

菜鸟学习javascript实例教程

2009-12-16 01:13| 发布者: admin| 查看: 69| 评论: 0|原作者: 回梦游仙

用JS显示文字的例子:





用HTML标签来格式化文本的例子:





书写JS位置的例子:
打开页面弹出窗口的例子








在BODY区内输出显示文本的例子:








调用其它的一个JS文件的例子:






The actual script is in an external script file called "xxx.js".





变量的使用

变量使用的例子:




This example declares a variable, assigns a value to it, and then displays the variable.


Then the variable is displayed one more time, only this time as a heading.





函数的例子
函数使用的一个例子:






onclick="myfunction()"
value="Call function">

By pressing the button, a function will be called. The function will alert a message.




带一个参数的函数的例子:






onclick="myfunction('Hello')"
value="Call function">

By pressing the button, a function with an argument will be called. The function will alert
this argument.




不同的两个参数调用函数的例子:






onclick="myfunction('Good Morning!')"
value="In the Morning">
onclick="myfunction('Good Evening!')"
value="In the Evening">


When you click on one of the buttons, a function will be called. The function will alert
the argument that is passed to it.




利用函数返回值的例子:






The script in the body section calls a function.


The function returns a text.





带参数的函数返回值的例子:







The script in the body section calls a function with two arguments, 2 and 3.


The function returns the sum of these two arguments.





条件语句的例子

简单条件语句的例子:




This example demonstrates the If statement.



If the time on your browser is less than 10,
you will get a "Good morning" greeting.




if ... else 的条件语句的例子:





This example demonstrates the If...Else statement.



If the time on your browser is less than 10,
you will get a "Good morning" greeting.
Otherwise you will get a "Good day" greeting.





用随机数产生连接的例子:





多条件的语句实现的例子:



This example demonstrates the switch statement.


You will receive a different greeting based on what day it is.


Note that Sunday=0, Monday=1, Tuesday=2, etc.





循环的例子:
for循环的一个例子:



Explanation:


The for loop sets i equal to 0.


As long as i is less than , or equal to, 5, the loop will continue to run.


i will increase by 1 each time the loop runs.




复杂循环的一个例子:





按条件循环while的例子:



Explanation:


i equal to 0.


While i is less than , or equal to, 5, the loop will continue to run.


i will increase by 1 each time the loop runs.




do...while循环的例子:



Explanation:


i equal to 0.


The loop will run


i will increase by 1 each time the loop runs.


While i is less than , or equal to, 5, the loop will continue to run.






字符串对象的例子:

检测字符串长度的例子:






检测子字符串位置的例子:



This example tests if a string contains a specified word. If the word is found it returns the position of the first character of the word in the original string. Note: The first position in the string is 0!




检测子字符串是否存在的例子:



This example tests if a string contains a specified word. If the word is found it returns the word.





取子字符串的例子:




The substr() method returns a specified part of a string. If you specify (2,6) the returned string will be from the second character (start at 0) and 6 long.



The substring() method also returns a specified part of a string. If you specify (2,6) it returns all characters from the second character (start at 0) and up to, but not including, the sixth character.




转换字符串的大小写






数组对象的实例

数组简单应用的例子:







另一种使用数组的方法:






使用数组的一些属性和方法:






数组的两个方法concat和slice





日期相关例子:

显示今天的日期:






显示当前的时间:





设置日期:





UTC时间:





显示当前的星期:





显示当前的日期和星期:





一个走动的时间:










数学对象的例子:







产生0-1之间的随机数的例子





产生0-10的随机数的例子






求最大数的例子:





求最小数的例子:





Convert Celsius to Fahrenheit






Insert a number in either input field, and the number will be converted into
either Celsius or Fahrenheit.




degrees Celsius

equals

degrees Fahrenheit



Note that the Math.round method is used,
so that the result will be returned as a whole number.



转变字符为数字的例子






Write a character:




The character's Unicode:





超级连接对象
用按钮来改变连接位置的例子:





Visit Microsoft






改变连接的打开方式的例子:





Visit W3Schools



Try the link before and after you have pressed the button!





使连接获得焦点和失去焦点






Visit W3Schools.com






连接打开的方式




Links and Anchors


Click on a button to display that anchor in window 2!











按钮对象

创建一个按钮











显示按钮的名称



The form's name is:








显示表单中各个项的名称






First name:


Last name:



value="Display name of form elements"
onClick="showFormElements(this.form)">




副选框的选择和取消













表单中的副选框的选择与取消






How would you like your coffee?


With cream

With sugar












副选框实现的功能(转换为大写)




First name:



Last name:



Convert fields to upper case




文档对象
显示连接的数量


First anchor

Second anchor

Third anchor



Number of anchors in this document:



显示当前所在服务器的地址


The domain name for this site is:




显示当前页面的地址:


The referrer of a document is the URL of the document that linked to a document.


The referrer of this document is:

In this case it is a frame page that is linking to this document. IE returns the URL of the frame page and Netscape returns the URL of the document linking to this document.





显示当前文档的标题


MY TITLE


The title of the document is:



用按钮来显示当前页面的地址











通过单击可以知道当前的标签





Click to see what element I am!





显示表单中元素的个数



















打开一个新的文档,显示几个文字











打开一个新的文档,并显示新的文档





Learning how to access the DOM is....







计算表单的个数



Name:


Age:





计算一个页面中图形的个数











显示表单的名字



Name:


Age:

You can use the form's number:



Or, the form's name (will not work in Netscape):






事件对象
单击弹出窗口





Click in the document. An alert box will alert which mouse button you clicked.





单击弹出窗口显示鼠标的位置





Click in the document. An alert box will alert the x and y coordinates of the cursor.





按一个键则显示该键的ASCII码





Note: Make sure the right frame has focus when trying this example!


Press a key on your keyboard. An alert box will alert the unicode of the key pressed.





单击任何地方显示鼠标在页面的坐标






Click somewhere in the document. An alert box will alert the x and y coordinates of the cursor, relative to the screen.





单击之后显示文档的鼠标坐标






Click somewhere in the document. An alert box will alert the x and y coordinates of the cursor.





显示SHIFT是否被按下






Click somewhere in the document. An alert box will tell you if you pressed the shift key or not.





单击显示我们页中的标签





Click somewhere in the document. An alert box will alert the tag name of the element you clicked on.


This is a header


This is a paragraph


Ball



显示事件发生的类型






Click on the document. An alert box will alert which type of event occurred.





表单对象
用表单显示地址:















显示表单所使用的方法






Name:






重置表单






Enter some text in the text fields and then press the "Reset form" button












提交表单






Firstname:

Lastname:







对email的验证






Enter your E-mail:



Note: This example ONLY tests if the e-mail address contains an "@" character. A "real-life" code
would have to test for punctuations, spaces and other things as well.





输入指定的数才能提交表单






Enter a value from 1 to 5:





输入特定长的字符才能提交表单






Enter some text (you will get a message if you add more than 5 characters):






对表单的检测






Enter your e-mail:

Enter a value from 1 to 5:

Enter your name, max 10 chararcters:






设置表单中的一项获得焦点












选择文本框中的文本












下拉列表框的取值






Select your favorite browser:




Your favorite browser is:




单选按钮的取值






Select which browser is your favorite:


Internet Explorer

Netscape

Opera








下拉列表的值的显示






Select numbers:








下拉列表的连接











光标自动跳到下一个文本区





This script automatically jumps to the next input field when the current field's maxlength has been reached.










Frame, Frameset 和 IFrame 对象
平分两个页面















Try to resize the frame.


Right-click inside the two frames and select "View Source" to see the source code.





是否显示滚动条















Right-click inside the frames and select "View Source" to see the source code.





改变页面的地址:














Right-click inside the two frames and select "View Source" to see the source code.




跳出框架






Click the button to break out of the frame:




更新两个页面














Right-click inside the two frames and select "View Source" to see the source code.




更新2个以上的页面

















Right-click inside the three frames and select "View Source" to see the source code.




更新两个IFRAME















图象对象
改变图象的高度












改变图象












改变图象的宽度












定位:
显示当前页的地址和改变当前页的地址












刷新页面










导航对象

检测你的浏览器






显示浏览器更加详细的信息





最详细的浏览器的信息






按浏览器不同实现导航








检测浏览器的版本








选择对象
设置下拉列表的可用性












返回下拉列表中选择的项的值











改变下拉列表的大小












列表可选择多项






Before you click on the "Select multiple" button, try to select more than one option (by holding down the shift or Ctrl key). Click on the "Select multiple" button and try again.









返回所选列表的文本值






Select your favorite fruit:









删除列表的项目












显示屏幕的信息






表格对象
改变表格的边框














100200
300400







改变表格的间距














100200
300400



最新评论

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-29 17:35 , Processed in 0.191274 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

返回顶部