Prototypes.js 使用

来源:互联网 发布:guts and glory mac 编辑:程序博客网 时间:2024/05/20 05:58

【1】使用$方法代替 document.getElementById

•var divElement = $(‘id); //返回id为”id”的元素对象

【2】使用$$方法获得元素引用

•$$方法同样用于获得元素的引用,与$不同的是,它接受一个css选择器作为参数,将所有符合该选择器的元素作为数组返回。

【3】根据css类名取得元素集合

【4】使用Try.these尝试运行多个函数

•Try.these(func1[,func2[,func3…]]);

【5】使用$F方法来获得表单元素的值

【6】数组迭代的简化方法

•array.each(function(arrayItem,arrayIndex)
•{
•//dosomething..
•});
【7】设置元素的样式
•var div = $(“divId”);
•div.setStyle({
•cssFloat:”left”,
•opacity:0.5
•});
【8】切换元素的隐现状态
•通过$方法获得的元素,可以使用hide方法来隐藏该元素,也可以使用show方法来显示隐藏的元素,还可以使用toggle方法来切换当前元素的隐现状态。
【9】序列化表单值
•使用$方法获取的表单元素,Prototype.js会对其扩展,增加serialize方法来完成对其表单项的序列化输出。
【10】转换HTML标签
•Prototype.js对String对象进行了扩展,并提供了escapeHTML方法来完成之一工作,其语法如下所示。
•var str = htmlstring.escapeHTML();
【11】基于Prototype.js的类和继承
【12】使用Class.create()创建类
•var class = Class.create();
【13】Prototype.js中的继承
•Prototype.js提供了Object.extend方法来复制对象的属性和方法。
【14】Prototype.js中的事件处理

注册事件处理函数
•Prototype.js提供了Event.observe来注册事件处理函数,其语法如下所示。
•Event.observe(element, eventName, handler[, useCapture = false])
注销事件处理函数
•使用Event.stopObserving方法可以注销一个事件处理函数,其语法如下所示。
•Event.stopObserving(element, eventName, handler[, useCapture = false])
【15】Prototype.js的Ajax功能
Ajax.Request方法详解
•newAjax.Request(url[,options]);
•newAjax.Updater(container, url[, options]);
•newAjax.PeriodicalUpdater(container, url[, options]);