asp.net c# Design Pattern WebService Windows Report Control Mobile Symbin Windows Phone 7 Visual Studio 2011

JavaScript使用方法和技巧(二)

Posted on 2007-03-26 16:46  笑缘  阅读(584)  评论(0编辑  收藏  举报

21 指定次数循环

1: <s cript>
2: <!--
3: var myArray = new Array(3);
4: myArray[0] = “Item 0”;
5: myArray[1] = “Item 1”;
6: myArray[2] = “Item 2”;
7: for (i = 0; i < myArray.length; i++) {
8: document.write(myArray[i] + “<br/>”);
9: }
10: // -->
11: </s cript>


22 设定将来执行

1: <s cript>
2: <!--
3: function hello() {
4: window.alert(“Hello”);
5: }
6: window.setTimeout(“hello()”,5000);
7: // -->
8: </s cript>


23 定时执行函数

1: <s cript>
2: <!--
3: function hello() {
4: window.alert(“Hello”);
5: window.setTimeout(“hello()”,5000);
6: }
7: window.setTimeout(“hello()”,5000);
8: // -->
9: </s cript>


24 取消定时执行

1: <s cript>
2: <!--
3: function hello() {
4: window.alert(“Hello”);
5: }
6: var myTimeout = window.setTimeout(“hello()”,5000);
7: window.clearTimeout(myTimeout);
8: // -->
9: </s cript>


25 在页面卸载时候执行函数

1: <body onUnload=”functionName();”>
2: Body of the page
3: </body>

Javas cript就这么回事2:浏览器输出


26 访问document对象

1: <s cript language=”Javas cript”>
2: var myURL = document.URL;
3: window.alert(myURL);
4: </s cript>


27 动态输出HTML

1: <s cript language=”Javas cript”>
2: document.write(“<p>Here’s some information about this document:</p>”);
3: document.write(“<ul>”);
4: document.write(“<li>Referring Document: “ + document.referrer + “</li>”);
5: document.write(“<li>Domain: “ + document.domain + “</li>”);
6: document.write(“<li>URL: “ + document.URL + “</li>”);
7: document.write(“</ul>”);
8: </s cript>


28 输出换行

1: document.writeln(“<strong>a</strong>”);
2: document.writeln(“b”);


29 输出日期

1: <s cript language=”Javas cript”>
2: var thisDate = new Date();
3: document.write(thisDate.toString());
4: </s cript>


30 指定日期的时区

1: <s cript language=”Javas cript”>
2: var myOffset = -2;
3: var currentDate = new Date();
4: var userOffset = currentDate.getTimezoneOffset()/60;
5: var timeZoneDifference = userOffset - myOffset;
6: currentDate.setHours(currentDate.getHours() + timeZoneDifference);
7: document.write(“The time and date in Central Europe is: “ + currentDate.toLocaleString());
8: </s cript>


31 设置日期输出格式

1: <s cript language=”Javas cript”>
2: var thisDate = new Date();
3: var thisTimeString = thisDate.getHours() + “:” + thisDate.getMinutes();
4: var thisDateString = thisDate.getFullYear() + “/” + thisDate.getMonth() + “/” + thisDate.getDate();
5: document.write(thisTimeString + “ on “ + thisDateString);
6: </s cript>


32 读取URL参数

1: <s cript language=”Javas cript”>
2: var urlParts = document.URL.split(“?”);
3: var parameterParts = urlParts[1].split(“&”);
4: for (i = 0; i < parameterParts.length; i++) {
5: var pairParts = parameterParts[i].split(“=”);
6: var pairName = pairParts[0];
7: var pairValue = pairParts[1];
8: document.write(pairName + “ :“ +pairValue );
9: }
10: </s cript>

你还以为HTML是无状态的么?

33 打开一个新的document对象

1: <s cript language=”Javas cript”>
2: function newDocument() {
3: document.open();
4: document.write(“<p>This is a New Document.</p>”);
5: document.close();
6: }
7: </s cript>


34 页面跳转

1: <s cript language=”Javas cript”>
2: window.location = http://www.liu21st.com/”;
3: </s cript>


35 添加网页加载进度窗口

1: <html>
2: <head>
3: <s cript language='javas cript'>
4: var placeHolder = window.open('holder.html','placeholder','width=200,height=200');
5: </s cript>
6: <title>The Main Page</title>
7: </head>
8: <body onLoad='placeHolder.close()'>
9: <p>This is the main page</p>
10: </body>
11: </html>


Javas cript就这么回事3:图像


36 读取图像属性

1: <img src="/”image1.jpg"” name=”myImage”>
2: <a href=”# ” onClick=”window.alert(document.myImage.width)”>Width</a>
3:


37 动态加载图像

1: <s cript language=”Javas cript”>
2: myImage = new Image;
3: myImage.src = “Tellers1.jpg”;
4: </s cript>


38 简单的图像替换

1: <s cript language=”Javas cript”>
2: rollImage = new Image;
3: rollImage.src = “rollImage1.jpg”;
4: defaultImage = new Image;
5: defaultImage.src = “image1.jpg”;
6: </s cript>
7: <a href="/”myUrl"” onMouseOver=”document.myImage.src = rollImage.src;”
8: onMouseOut=”document.myImage.src = defaultImage.src;”>
9: <img src="/”image1.jpg"” name=”myImage” width=100 height=100 border=0>


39 随机显示图像

1: <s cript language=”Javas cript”>
2: var imageList = new Array;
3: imageList[0] = “image1.jpg”;
4: imageList[1] = “image2.jpg”;
5: imageList[2] = “image3.jpg”;
6: imageList[3] = “image4.jpg”;
7: var imageChoice = Math.floor(Math.random() * imageList.length);
8: document.write(‘<img src=”’ + imageList[imageChoice] + ‘“>’);
9: </s cript>


40 函数实现的图像替换

1: <s cript language=”Javas cript”>
2: var source = 0;
3: var replacement = 1;
4: function createRollOver(originalImage,replacementImage) {
5: var imageArray = new Array;
6: imageArray[source] = new Image;
7: imageArray[source].src = originalImage;
8: imageArray[replacement] = new Image;
9: imageArray[replacement].src = replacementImage;
10: return imageArray;
11: }
12: var rollImage1 = createRollOver(“image1.jpg”,”rollImage1.jpg”);
13: </s cript>
14: <a href=”#” onMouseOver=”document.myImage1.src = rollImage1[replacement].src;”
15: onMouseOut=”document.myImage1.src = rollImage1[source].src;”>
16: <img src="/”image1.jpg"” width=100 name=”myImage1” border=0>
17: </a>

Copyright © 2024 笑缘
Powered by .NET 9.0 on Kubernetes

asp.net c# Design Pattern WebService Windows Report Control Mobile Windows Phone Visual Studio
asp.net c# Design Pattern WebService Windows Report Control Mobile