2009年10月27日星期二

100多个很有用的JavaScript函数以及基础写法大集合

FROM: 100多个很有用的JavaScript函数以及基础写法大集合


  • 输出空语句 document.write("");

  • 得到表单中元素的名称和值 document.getElementById("表单中元素的ID号").name(或value);

  • 注释为 //

  • 传统的HTML文档顺序是 document > html > (head,body)

  • 一个浏览器窗口中的DOM顺序是 window > (navigator,screen,history,location,document)

  • 值类型 String, Number, Boolean, Null, Object, Function

  • 数字转换成字符型 "" + 变量

  • 比较操作符有 ==等于, !=不等于, >, >=, <, <=

  • 声明变量使用 var来进行声明

  • 判断语句结构 if(condition){}else{}

  • 循环结构 for([initial expression];[condition];[upadte expression]) {inside loop}

  • 循环中止的命令是 break

  • 函数定义 function functionName([parameter],...){statement[s]}

  • 关闭文档的输出流 document.close();

  • 创建一个文档元素 document.createElement(), document.createTextNode()

  • 得到元素的方法 document.getElementById()

  • 复选按钮在JS中判断是否选中 document.forms[0].checkThis.checked (checked属性代表为是否选中返回TRUE或FALSE)

  • 单选按钮组(单选按钮的名称必须相同), 取单选按钮组的长度 document.forms[0].groupName.length

  • 单选按钮组判断是否被选中也是用 checked

  • 下拉列表框的值 document.forms[0].selectName.options[n].value (n有时用下拉列表框名称加上, selectedIndex来确定被选中的值)

  • 表示当前所属的位置 this

  • 当在超链接中调用 JS 函数时用 javascript 来开头后面加函数名

  • 在老的浏览器中不执行此JS<!-- //-->

  • 引用一个文件式的JS <script type="text/javascript" src="test.js"></script>

  • 指定在不支持脚本的浏览器显示的HTML <noscript></noscript>

  • 当超链和 ONCLICK 事件都有时, 则老版本的浏览器转向 a.html, 否则转向 b.html. <a href="a.html" onclick="location.href='b.html';return false">test</a>

  • JS的内建对象有 Array, Boolean, Date, Error, EvalError, Function, Math, Number, Object, RangeError, ReferenceError, RegExp, String, SyntaxError, TypeError, URIError

  • 换行 \n

  • all 代表其下层的全部元素

  • 焦点顺序 document.getElementByid("表单元素").tabIndex = 1

  • innerHTML 取得的值是表单元素的值

  • innerTEXT 取得的值和上面的一样,只不过不会把 <em> 这种标记显示出来

  • contentEditable 可设置元素是否可被修改, isContentEditable返回是否可修改的状态

  • isDisabled 判断是否为禁止状态, disabled设置禁止状态

  • addBehavior() JS调用的外部函数文件,其扩展名为.htc

  • window.focus() 使当前的窗口在所有窗口之前

  • blur()指失去焦点, 与focus()相反

  • select() 指元素为选中状态

  • 防止用户对文本框中输入文本 onfocus="this.blur()"

  • 取出该元素在页面中出现的数量 document.all.tags("div(或其它HTML标记符)").length

  • 添加到收藏夹 external.AddFavorite("http://www.google.com","Google");

  • 设置时间间隔 setInterval("expr", msecDelay)或setInterval(funcRef, msecDelay)或setTimeout

  • 模态显示在IE4+行, showModalDialog("URL"[,arguments][,features]);


  • document.write() 不换行的输出, document.writeln() 换行输出

  • document.body.noWrap=true; 防止链接文字折行

  • parseInt(string[,radix(代表进制)]) 强制转换成整型

  • parseFloat(string[,radix]) 强制转换成浮点型

  • isNaN(变量) 测试是否为数值型

  • 定义常量的关键字 const

  • 当文件中出现多个 form 表单时.可以用 document.forms[0], document.forms[1]来代替




数学函数



  • Math.PI() 返回圆周率

  • Math.SQRT2() 返回开方

  • Math.max(value1, value2) 返回两个数中的最大值

  • Math.pow(value1, 10) 返回value1的十次方

  • Math.round(value1) 四舍五入函数

  • Math.floor(Math.random()*(n + 1)) 返回随机数





日期函数列表



  • var today = new Date(); 定义日期型变量

  • dateObj.getTime() 得到时间

  • dateObj.getYear() 得到年份

  • dateObj.getFullYear() 得到四位的年份

  • dateObj.getMonth() 得到月份
  • ,
  • dateObj.getDate() 得到日

  • dateObj.getDay() 得到日期几

  • dateObj.getHours() 得到小时

  • dateObj.getMinutes() 得到分

  • dateObj.getSeconds() 得到秒

  • dateObj.setTime(value) 设置时间

  • dateObj.setYear(value) 设置年

  • dateObj.setMonth(value) 设置月

  • dateObj.setDate(value) 设置日

  • dateObj.setDay(value) 设置星期几

  • dateObj.setHours 设置小时

  • dateObj.setMinutes(value) 设置分

  • dateObj.setSeconds(value) 设置秒(注意 此日期时间从0开始计)



窗口 window



  • 遇到脚本错误时不做任何操作 window.onerror = doNothing;, 指定错误句柄的语法为 window.onerror = handleError;

  • 分为两种窗体输出 模态和非模态 window.showModaldialog(), window.showModeless()

  • 状态栏文字的设置 window.status='文字', 默认的状态栏文字设置 window.defaultStatus = '文字.';

  • FRAME的表示方式 [window.]frames[n].ObjFuncVarName,frames["frameName"].ObjFuncVarName,frameName.ObjFuncVarName

  • parent 代表父亲对象, top代表最顶端对象

  • JS中指定当前打开窗口的父窗口 window.opener, 支持opener.opener...的多重继续

  • 打开窗口window.open()

  • 关闭一个窗口 window.close()

  • 窗口本身 self

  • 弹出提示信息 window.alert("字符")

  • 弹出确认框 window.confirm();

  • 提示输入框 window.prompt("message","defaultReply");

  • 关闭当前的窗口 window.close();

  • 窗口重定向 window.navigate("http://www.google.com");

  • 打印 window.print()

  • 窗口滚动条 window.scroll(x,y)

  • 窗口滚动到位置 window.scrollby

  • 指定当前显示链接的位置 window.location.href="URL"

  • 取出窗体中的所有表单的数量 document.forms.length

  • 当窗体第一次调用时使用的文件句柄 onload()

  • 当窗体关闭时调用的文件句柄 onunload()

  • window.location.reload() 刷新当前页面

  • parent.location.reload() 刷新父亲对象(用于框架)

  • opener.location.reload() 刷新父窗口对象(用于单开窗口)

  • top.location.reload() 刷新最顶端对象(用于多开窗口)

  • window.history.back() 返回上一页

  • window.history.forward() 返回下一页

  • window.history.go(n|history-url) 返回第 n 页, 也可以使用访问过的URL




字符串



  • var myString = new String("This is lightsword"); 字符串的定义

  • string1.indexOf("string2") != -1 返回字符串2在字符串1中出现的位置, -1 则说明没找到

  • stringA.substring(2, 6); 取出字符串中指定起点和终点的子字符串

  • string.charAt(n) 取该变量的第 n 位的字符

  • string.charCodeAt( n ) 返回第 n 个字符的ASCII码值

  • string.concat(string2) 字符串连接, 或用+=进行连接

  • string.indexOf(searchString, startIndex) 返回第一个出现的位置(从0开始计算)

  • string.lastIndexOf(searchString[, startIndex]) 最后一次出现的位置

  • string.match(regExpression) 判断字符是否匹配

  • string.replace(regExpression, replaceString) 替换现有字符串

  • string.split(separator) 返回一个数组存储值

  • string.substr(start[,length]) 取从第几位到指定长度的字符串

  • string.toLowerCase() 使字符串全部变为小写

  • string.toUpperCase() 使全部字符变为大写

  • string.length 取字符串长度



window.location 属性



  • protocol() http

  • hostname www.google.com

  • port 80

  • host www.google.com 80

  • pathname /index.html

  • hash #test 指跳转到相应的锚记

  • href 全部信息http://www.google.com./index.html



设置表单中所有文本型的成员的值为空
[js]
var form = window.document.forms[0]
for (var i = 0; i < form.elements.length; i++) {
if (form.elements[i].type == "text"){
form.elements[i].value = "";
}
}
[/js]

窗口全屏大小
[js]
function fullScreen( ) {
this.moveTo(0,0);
this.outerWidth = screen.availWidth;
this.outerHeight = screen.availHeight;
}
window.maximize=fullScreen;
[/js]

退出之前使用的句柄
[js]
function verifyClose(){
event.returnValue="we really like you and hope you will stay longer.";
}
window.onbeforeunload = verifyClose;
[/js]

没有评论:

发表评论