分享

常用的 Javascript 操作汇总 (二)

 WindySky 2009-06-24

17 定义函数

1: <script language=”JavaScript”>
2: <!--
3: function multiple(number1,number2) {
4: var result = number1 * number2;
5: return result;
6: }

7: // -->
8: </script>



18 调用JS函数

1: <a href=”#” onClick=”functionName()”>Link text</a>
2: <a href="/”javascript:functionName"()”>Link text</a>



19 在页面加载完成后执行函数

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


20 条件判断

1: <script>
2: <!--
3: var userChoice = window.confirm(“Choose OK or Cancel”);
4: var result = (userChoice == true) ? “OK” : “Cancel”;
5: document.write(result);
6: // -->
7: </script>



21 指定次数循环

1: <script>
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 +<br>”);
9: }

10: // -->
11: </script>



22 设定将来执行

1: <script>
2: <!--
3: function hello() {
4: window.alert(“Hello”);
5: }

6: window.setTimeout(“hello()”,5000);
7: // -->
8: </script>



23 定时执行函数

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

7: window.setTimeout(“hello()”,5000);
8: // -->
9: </script>



24 取消定时执行

1: <script>
2: <!--
3: function hello() {
4: window.alert(“Hello”);
5: }

6: var myTimeout = window.setTimeout(“hello()”,5000);
7: window.clearTimeout(myTimeout);
8: // -->
9: </script>

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

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

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


26 访问document对象

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



27 动态输出HTML

1: <script language=”JavaScript”>
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: </script>


28 输出换行

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多