日期:2014-05-16  浏览次数:20399 次

JS方法与技巧

JS方法与技巧
2010年08月03日
  JavaScript就这么回事1:基础知识 
  1 创建脚本块
  1: 
  2: JavaScript code goes here
  3:  
  2 隐藏脚本代码
  1: 
  2: 
  5:  
  在不支持JavaScript的浏览器中将不执行相关代码
  3 浏览器不支持的时候显示
  1: 
  2: Hello to the non-JavaScript browser.
  3:  
  4 链接外部脚本文件
  1:  
  5 注释脚本
  1: // This is a comment
  2: document.write("Hello"); // This is a comment
  3: /*
  4: All of this
  5: is a comment
  6: */ 
  6 输出到浏览器
  1: document.write("Hello"); 
  7 定义变量
  1: var myVariable = "some value"; 
  8 字符串相加
  1: var myString = "String1" + "String2"; 
  9 字符串搜索
  1: 
  2: 
  7:  
  10 字符串替换
  1: thisVar.replace("Monday","Friday"); 
  11 格式化字串
  1: 
  2: ");
  5: document.write(myVariable.blink() + "");
  6: document.write(myVariable.bold() + "");
  7: document.write(myVariable.fixed() + "");
  8: document.write(myVariable.fontcolor("red") + "");
  9: document.write(myVariable.fontsize("18pt") + "");
  10: document.write(myVariable.italics() + "");
  11: document.write(myVariable.small() + "");
  12: document.write(myVariable.strike() + "");
  13: document.write(myVariable.sub() + "");
  14: document.write(myVariable.sup() + "");
  15: document.write(myVariable.toLowerCase() + "");
  16: document.write(myVariable.toUpperCase() + "");
  17: 
  18: var firstString = "My String";
  19: var finalString = firstString.bold().toLowerCase().fontcolor("red");
  20: // -->
  21:  
  12 创建数组
  1: 
  2: 
  11:  
  13 数组排序
  1: 
  2: 
  11:  
  14 分割字符串
  1: 
  2: 
  10:  
  15 弹出警告信息
  1: 
  2: 
  5:  
  16 弹出确认框
  1: 
  2: 
  5:  
  17 定义函数
  1: 
  2: 
  8:  
  18 调用JS函数
  1: Link text
  2: Link text 
  19 在页面加载完成后执行函数
  1: 
  2: Body of the page
  3:  
  20 条件判断
  1: 
  2: 
  7:  
  21 指定次数循环
  1: 
  2: ");
  9: }
  10: // -->
  11:  
  22 设定将来执行
  1: 
  2: 
  8:  
  23 定时执行函数
  1: 
  2: 
  9:  
  24 取消定时执行
  1: 
  2: 
  9:  
  25 在页面卸载时候执行函数
  1: 
  2: Body of the page
  3:  
  JavaScript就这么回事2:浏览器输出 
  26 访问document对象
  1: 
  2: var myURL = document.URL;
  3: window.alert(myURL);
  4:  
  27 动态输出HTML
  1: 
  2: document.write("Here's some information about this document:");
  3: document.write("");
  4: document.write("Referring Document: " + document.referrer + "");
  5: document.write("Domain: " + document.domain + "");
  6: document.write("URL: " + document.URL + "");
  7: document.write("");
  8:  
  28 输出换行
  1: document.writeln("a");
  2: document.writeln("b"); 
  29 输出日期
  1: 
  2: var thisDate = new Date();
  3: document.write(thisDate.toString());
  4:   30 指定日期的时区
  1: 
  2: var myOffset = -2;
  3: var currentDate = new Date();
  4: var userOffset = currentDate.getTimezoneOffset()/60;
  5: var timeZoneDifference = userOffset - myOffset;
  6: currentDate.setHo