Prototype 可获取浏览器的一些属性
Version Prototype.Version
Browser
Gecko [bool] Prototype.Browser.Gecko
IE [bool] Prototype.Browser.IE
MobileSafari [bool] Prototype.Browser.MobileSafari
Opera [bool] Prototype.Browser.Opera
WebKit [bool] Prototype.Browser.WebKit
BrowserFeatures
ElementExtensions [bool]
SelectorsAPI [bool]
SpecificElementExtensions [bool]
XPath [bool]
Class 创建类
create
var Widget = Class.create({
initialize: function() {
alert(1);
}
});
var StarWidget = Class.create(Widget, {
initialize: function($super) {
$super();
alert(2);
}
});
Methods
addMethods
Abstract 抽象类,不能直接使用
EventObserver
TimedObserver
Object
clone [function] 克隆一个对象 var newObj = Object.clone({text: "hello"})
extend [function] 扩展属性 Object.extend({}, {text: "hello"})
inspect [function] 转换为字符串 Object.inspect(new Date())
isArray [function] Object.isArray([])
isElement [function] Object.isElement(window)
isFunction [function] Object.isFunction(alert)
isHash [function] Object.isHash(new Hash({ }))
isNumber [function] Object.isNumber(12)
isString [function] Object.isString("hello")
isUndefined [function] Object.isUndefined(xxx)
keys [function] Object.keys({text: "hello"})
toHTML [function] Object.toHTML({toHTML: function() {return "HTML"}})
toJSON [function] Object.toJSON({})
toQueryString [function] Object.toQueryString({text: "hello"})
values [function] Object.values({text: "hello"})
Function
argumentNames [function] 获得参数名称列表 alert.argumentNames()
bind [function] (function() {}).bind(this)
bindAsEventListener [function] (function() {}).bindAsEventListener(this)
curry [function] 可用来预填入参数,起到重载函数的作用。function f(a,b) {}; var fa = f.curry(1); //fa(2) == f(1,2)
defer [function] 延迟执行函数,等到系统不忙的时候执行 (function() {}).defer();
delay [function] 延迟执行函数,可指定延迟时间 (function(a) {}).delay(1, ""); //延迟1秒执行
methodize [function] 为方法补充一个this参数 var f = (function() {alert(arguments[0]);alert(arguments[1]);}).methodize; f(1);
wrap [function] 提供AOP的方法 a.test = a.test.wrap(function(f) { alert("begin");f();alert("end"); });
Date
toJSON (new Date()).toJSON()
Try
these Try.these(function() {return "1";}, function() {return "2";})
RegExp
match /abc/.match("abcd") == /abc/.test("abcd")
escape RegExp.escape("C:\\WINDOWS")
PeriodicalExecuter timer的封装 var timer = new PeriodicalExecuter(function() {alert(1);}, 3); //每3秒执行一次
stop timer.stop();
String
interpret 强制转换类型为字符串 String.interpret(null)
gsub 全部替换 'click dblclick mousedown mouseup mouseover mousemove mouseout'.gsub(/\s+/, ",")
sub 指定替换 'click dblclick mousedown mouseup mouseover mousemove mouseout'.sub(/\s+/, ",", 2) //位置可指定
scan 列举满足pattern的子字符串 'apple, pear & orange'.scan(/\w+/, alert);
truncate 截断字符串 "ddddddddddddddddddd".truncate(8,"...")
strip 去掉首尾空格 " abc ".strip()
stripTags 去掉html符号 " a<b>c</b> ".stripTags()
stripScripts 去掉其中的脚本块
extractScripts 获取其中的脚本块
evalScripts 执行其中的脚本块 '<script>2 + 2</script><script>alert("hello world!")</script>'.evalScripts();
escapeHTML '<div class="article">This is an article</div>'.escapeHTML();
unescapeHTML
toQueryParams 'http://www.example.com?section=blog&id=45#comments'.toQueryParams();
parseQuery == toQueryParams
toArray 'hello world!'.toArray()
succ 'abc'.succ() => abd
times 'a'.times(3) => aaa
camelize 'background-color'.camelize();
capitalize 'backgroundcolor'.capitalize();
underscore 'borderBottomWidth'.underscore()
dasherize 'borderBottomWidth'.underscore().dasherize()
inspect 'abc'.inspect() == 'abc' 'abc'.inspect(true) ==> "abc"
toJSON 'abc'.toJSON()
unfilterJSON '/*-secure-\n{"name": "Violet", "occupation": "character", "age": 25}\n*/'.unfilterJSON()
isJSON "{a:\"1\"}".isJSON()
evalJSON "{a:\"1\"}".evalJSON() "{a:\"1\"}".evalJSON(true)
include "abcdefg".include("abc")
startsWith "abc".startsWith("ab")
endsWith "abc".endsWith("bc")
empty "".empty()
blank " ".blank()
interpolate "#{animals} on a #{transport}".interpolate({ animals: "Pigs", transport: "Surfboard" });
Hash 哈希表 var hash = new Hash() var hash = new Hash({a:1,b:2})
set hash.set("cn", "中国")
get hash.get("cn")
unset hash.unset("cn")
toObject hash.toObject()
keys hash.keys()
values hash.values()
index hash.index("中国")
merge hash.merge({a:1,b:2}) //不改变原Hash对象
update hash.update({a:1,b:2})
toQueryString hash.toQueryString()
inspect hash.inspect()
toJSON hash.toJSON()
clone hash.clone()
from == $H Hash.from() 新创建一个Hash对象
Enumerable 可枚举对象
each [1,2].each(function(value,index) { alert(value); }, this)
eachSlice 分组 [1,2,3,4].each(2, function(value,index) { alert(value); }, this) //返回分组后的值
every == all [1,2].all(function(value,index) { return value > 0 }, this) //返回布尔值
some == any [1,2].all(function(value,index) { return value > 1 }, this) //返回布尔值
map == collect [1,2].collect(function(value,index) { return value + 1; }, this) //返回相同数量的元素数组
find == detect [1,2].detect(function(value,index) { return value > 1; }, this) //返回第一个满足条件的元素
filter == select == findAll [1,2].findAll(function(value,index) { return value > 1; }, this) //返回全部满足条件的元素
grep ["a", "b", "c"].grep(/a/, function(value,index) { return value.toUpperCase();}, this)
member == include [1,2,3].include(1)
inGroupsOf [1,2,3].inGroupsOf(2, 0) //按数量分组,不足则补0
inject [1,2,3].inject(0, function(memo, value, index) { return memo + value}, this) //返回一个值,通常可用来求和
invoke [1,2,3].invoke("toString")
max [1,2,3].max() [1,2,3].max(function(value, index) { return value * value; })
min [1,2,3].min() [1,2,3].min(function(value, index) { return 0 - value; })
partition 二分 [1,2,3].partition(function(value, index) { return value > 2; })
pluck [{text:1}, {text:2}].pluck("text")
reject 基本上与findAll是相反的 [1,2].reject(function(value,index) { return value > 1; }, this)
sortBy [0,1,2].sortBy(function(value, index) {return -value;}, this)
entries == toArray [0,1,2].toArray()
zip [1,2,3].zip(function(value) {return value}) [1,2,3].zip([4,5,6],[7,8,9])
size [1,2,3].size()
inspect [1,2,3].inspect()
Array
from == $A
clear [1].clear()
first [1,2,3].first()
last [1,2,3].last()
compact [1,2,3,null].compact()
flatten [1,2,3,[1,2,3]].flatten()
without [1,2,3].without(1,2)
reverse [1,2,3].reverse() [1,2,3].reverse(false) //不改变原数组
reduce [1].reduce() ==> 1
uniq [1,2,1,11,11,2,1,1].uniq() [1,1,2,2,1].uniq(true)
intersect 取子集 [1,2,3,4].intersect([3,4,5])
clone [1,2,3].clone()
size [1,2].size()
inspect [1,2,3].inspect()
toJSON [1,2,3].toJSON()
indexOf [1,2,3,1,2,3].indexOf(3)
lastIndexOf [1,2,3,1,2,3].lastIndexOf(3,5)
toArray [1,2,3].toArray()
$w $w("12121 3123")
Number
toColorPart (111000).toColorPart()
succ (1).succ()
times (3).times(function(value, index) {alert(1);}, this)
toPaddedString (100).toPaddedString(3, 16)
toJSON (100).toJSON()
abs (-1).abs()
round (1.23).round()
ceil (1.23).ceil()
floor (1.23).floor()
ObjectRange new ObjectRange(start, end, exclusive)
$R 创建一个枚举对象 $R(1,10, true).each(function(v) {alert(v);})
Ajax
getTransport Ajax.getTransport
activeRequestCount Ajax.activeRequestCount 激活的请求数
new Ajax.Request("ttt", "maintain.html", {});
new Ajax.Updater("ttt", "maintain.html");
new Ajax.Updater('bookdiv',"foo.jsp", {onComplete: initObserve, asynchronous:true});
new Ajax.Updater("divSiteHead", "/maintain.html", {method: "post", asynchronous: true, contentType: "application/x-www-form-urlencoded", encoding: "UTF-8", parameters: "", evalJSON: true, evalJS: true, insertion: "after", onSuccess: function(response, json) { alert(response.responseText); }});
Element
visible $("txtSearchKeyWord").visible()
toggle $("txtSearchKeyWord").toggle()
hide $("txtSearchKeyWord").hide()
show $("txtSearchKeyWord").show()
remove $("txtSearchKeyWord").remove()
update $("divContent").update("<b>dasdsad</b>") //此方法支持脚本 等于innerHTML
replace $("divContent").replace("<b>dasdsad</b>") //也可替换元素 等于outterHTML
insert $("txtSearchKeyWord").insert({ before : '<b>Check this stuff out</b>'}) //before, after, top, bottom
wrap $("txtSearchKeyWord").wrap("div", {style:"color:red"}) //将元素外包上另一个父节点
inspect $("txtSearchKeyWord").inspect()
recursivelyCollect $("txtSearchKeyWord").recursivelyCollect("parentNode")
ancestors $("txtSearchKeyWord").ancestors() 父节点
descendants $("txtSearchKeyWord").parentNode.descendants() 子节点
firstDescendant $("txtSearchKeyWord").firstDescendant() 第一个子节点
childElements == immediateDescendants $("txtSearchKeyWord").immediateDescendants() 子节点(仅下面第一层) //已被childElements取代
previousSiblings $("txtSearchKeyWord").previousSiblings()
nextSiblings $("txtSearchKeyWord").nextSiblings()
siblings $("txtSearchKeyWord").siblings() //== previousSiblings + nextSiblings
match $("txtSearchKeyWord").match("input")
up $("txtSearchKeyWord").up("div") $("txtSearchKeyWord").up("div", 1) $("txtSearchKeyWord").up()
down $("txtSearchKeyWord").down("div") $("txtSearchKeyWord").down("div", 1) $("txtSearchKeyWord").down()
previous $("txtSearchKeyWord").previous("div", 1)
next $("txtSearchKeyWord").next("div", 1)
getElementsBySelector==select $("txtSearchKeyWord").up().select("input", "div") //子节点
adjacent $("txtSearchKeyWord").adjacent("input", "div") //子节点,不包括自己
identify $("txtSearchKeyWord").identify();
readAttribute $("txtSearchKeyWord").readAttribute("id")
writeAttribute $("txtSearchKeyWord").writeAttribute("test", 123)
getHeight $("txtSearchKeyWord").getHeight()
getWidth $("txtSearchKeyWord").getWidth()
classNames $("txtSearchKeyWord").classNames() //已废弃
hasClassName $("txtSearchKeyWord").hasClassName("dddd")
addClassName $("txtSearchKeyWord").addClassName("newstyle")
removeClassName $("txtSearchKeyWord").removeClassName("newstyle")
toggleClassName $("txtSearchKeyWord").toggleClassName("newstyle")
cleanWhitespace //去掉那些无内容的空节点 $("txtSearchKeyWord").cleanWhitespace()
empty $("txtSearchKeyWord").empty() //其innerHTML是否无内容
descendantOf $("txtSearchKeyWord").descendantOf(document.body)
scrollTo $("txtSearchKeyWord").scrollTo()
getStyle $("txtSearchKeyWord").getStyle("backgroundColor")
getOpacity $("txtSearchKeyWord").getOpacity()
setStyle $("txtSearchKeyWord").setStyle("color:red") $("txtSearchKeyWord").setStyle({color: "blue"})
setOpacity $("txtSearchKeyWord").setOpacity(0.5)
getDimensions $("txtSearchKeyWord").getDimensions() => {width: xx, height: yy}
makePositioned $("txtSearchKeyWord").makePositioned() //position = relative
undoPositioned $("txtSearchKeyWord").undoPositioned()
makeClipping $("txtSearchKeyWord").makeClipping() //overflow = "hidden"
undoClipping $("txtSearchKeyWord").undoClipping() //overflow = ""
cumulativeOffset $("txtSearchKeyWord").cumulativeOffset()
positionedOffset $("txtSearchKeyWord").positionedOffset()
absolutize $("txtSearchKeyWord").absolutize()
relativize $("txtSearchKeyWord").relativize()
cumulativeScrollOffset $("txtSearchKeyWord").cumulativeScrollOffset()
getOffsetParent $("txtSearchKeyWord").getOffsetParent()
viewportOffset $("txtSearchKeyWord").viewportOffset()
clonePosition $("txtSearchKeyWord").clonePosition(anotherElement)
hasAttribute $("txtSearchKeyWord").hasAttribute("id")
document.viewport
getDimensions document.viewport.getDimensions()
getWidth document.viewport.getWidth()
getHeight document.viewport.getHeight()
getScrollOffsets document.viewport.getScrollOffsets()
Selector 选择器
$$
$$("input") = document.getElementsByTagName("input")
$$('input[value="1"]') 指定属性的
$$('input.textbox') 指定样式的
$$('input.tbText') 指定名字的
Form
serialize $("frmSerach").serialize({hash: false, submit: true})
$F == getValue $("txtSearchKeyWord").getValue() 取值
focusFirstElement $("frmSerach").focusFirstElement()
reset $("frmSerach").reset()
getElements $("frmSerach").getElements()
getInputs $("form").getInputs() $("form").getInputs("hidden", "name")
disable $("frmSerach").disable()
enable $("frmSerach").enable()
findFirstElement $("frmSerach").findFirstElement()
request $("frmSerach").request() $("frmSerach").request({parameters: {a: 1, b: 2}, method: "post"})
Form.Element = Field
focus $("tbText").focus()
select $("tbText").select()
serialize $("tbText").serialize()
$F = getValue $("tbText").getValue()
setValue $("tbText").setValue()
clear $("tbText").clear()
present $("tbText").present() //返回布尔值
activate $("tbText").activate()
disable $("tbText").disable()
enable $("tbText").enable()
$F $F("txtSearchKeyWord") 取值
Event 使用了Event.observe则event已被自动扩展
observe Event.observe(document.body, 'click', function(event) {})
stopObserving Event.stopObserving(document.body, 'click', func) Event.stopObserving(document.body, 'click') Event.stopObserving(document.body)
extend Event.extend(evt) 只有如 οnclick=click() 之类的才需要调此方法
element function(event) {alert(event.element());}
findElement function(event) {alert(event.findElement("DIV"));} //注意:找不到的话,会返回document
isLeftClick function(event) {alert(event.isLeftClick());}
pointerX function(event) {alert(event.pointerX());}
pointerY function(event) {alert(event.pointerY());}
stop function(event) {event.stop();}
unloadCache 已经被废弃的方法
fire $("abc").fire("click");
document.observe('dom:loaded',function(){alert("Dom loaded");})