prototype.js 参考(五)

本文详细介绍了Ajax对象及其相关类的功能,包括Ajax请求的管理、响应处理、动态内容插入等关键特性。同时还探讨了如何利用Ajax实现页面局部刷新及周期性更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Ajax object
这个对象被用作其他提供AJAX功能的类的根对象。
Property Type Kind Description
activeRequestCount Number instance 正在处理中的Ajax请求的个数。
Method Kind Arguments Description
getTransport() instance (none) 返回新的XMLHttpRequest 对象。
The Ajax.Responders object
继承自 Enumerable
这个对象维持一个在Ajax相关事件发生时将被调用的对象的列表。比如,你要设置一个全局钩子来处理Ajax操作异常,那么你就可以使用这个对象。
Property Type Kind Description
responders Array instance 被注册到Ajax事件通知的对象列表。
Method Kind Arguments Description
register(responderToAdd) instance responderToAdd: object with methods that will be called. 被传入参数的对象应包含名如Ajax事件的系列方法(如onCreate,onComplete,onException)。通讯事件引发所有被注册的对象的合适名称的函数被调用。
unregister(responderToRemove) instance responderToRemove: object to be removed from the list. 从列表中移除。
dispatch(callback, request, transport, json) instance callback: name of the AJAX event being reported, request: the Ajax.Request object responsible for the event, transport: the XMLHttpRequest object that carried (or is carrying) the AJAX call, json: the X-JSON header of the response (if present) 遍历被注册的对象列表,找出有由callback参数决定的那个函数的对象。然后向这些函数传递其它的三个参数,如果Ajax响应中包含一个含有JSON内容的X-JSON HTTP头,那么它会被热行并传入json参数。如果事件是onException,那么transport参数会被异常代替,json不会传递。
The Ajax.Base class
这个类是其他在Ajax对象中定义的类的基类。
Method Kind Arguments Description
setOptions(options) instance options: AJAX options
设定AJAX操作想要的选项。
responseIsSuccess() instance (none) 返回 true 如果AJAX操作成功,否则为 false 。
responseIsFailure() instance (none) 与 responseIsSuccess() 相反。
The Ajax.Request class
继承自 Ajax.Base
封装 AJAX 操作
Property Type Kind Description
Events Array static 在AJAX操作中所有可能报告的事件/状态的列表。这个列表包括: 'Uninitialized', 'Loading', 'Loaded', 'Interactive', 和 'Complete'。
transport XMLHttpRequest instance 承载AJAX操作的 XMLHttpRequest 对象。
url string instance 请求的URL。
Method Kind Arguments Description
[ctor](url, options) constructor url: the url to be fetched, options: AJAX options 创建这个对象的一个实例,它将在给定的选项下请求url。onCreate事件在调用constructor事被激发。 重要: 如果选择的url受到浏览器的安全设置,他会一点作用也不起。 很多情况下,浏览器不会请求与当前页面不同主机(域名)的url。 你最好只使用本地url来避免限制用户配置他们的浏览器(谢谢Clay)
evalJSON() instance (none) 这个方法显然不会被外部调用。它在Ajax响应中含有X-JSON HTTP头时用于内部调用执行这些内容。
evalReponse() instance (none) 这也方法显然不会被外部调用,如果Ajax响应含有一个值为text/javascript的Cotent-Type头,那么这个方法就用被调用执行响应体。
header(name) instance name: HTTP header name 引用Ajax响应的头的内容,在Ajax访问结束后再调用这个方法。
onStateChange() instance (none) 这个方法通常不会被外部调用。 当AJAX请求状态改变的时候被这个对象自己调用。
request(url) instance url: url for the AJAX call 这个方法通常不会被外部调用。已经在构造方法中调用了。
respondToReadyState(readyState) instance readyState: state number (1 to 4) 这个方法通常不会被外部调用。 当AJAX请求状态改变的时候被这个对象自己调用。
setRequestHeaders() instance (none) 这个方法通常不会被外部调用。 被这个对象自己调用来配置在HTTP请求要发送的HTTP报头。
The options argument object
An important part of the AJAX operations is the options argument. There's no options class per se. Any object can be passed, as long as it has the expected properties. It is common to create anonymous objects just for the AJAX calls.
Property Type Default Description
method String 'post' HTTP 请求方式。
parameters String '' 在HTTP请求中传入的url格式的值列表。
asynchronous Boolean true 指定是否做异步 AJAX 请求。
postBody String undefined 在HTTP POST的情况下,传入请求体中的内容。
requestHeaders Array undefined 和请求一起被传入的HTTP头部列表, 这个列表必须含有偶数个项目, 任何奇数项目是自定义的头部的名称, 接下来的偶数项目使这个头部项目的字符串值。 例子:['my-header1', 'this is the value', 'my-other-header', 'another value']
onXXXXXXXX Function(XMLHttpRequest, Object) undefined 在AJAX请求中,当相应的事件/状态形成的时候调用的自定义方法。 例如 var myOpts = {onComplete: showResponse, onLoaded: registerLoaded};. 这个方法将被传入一个参数, 这个参数是承载AJAX操作的 XMLHttpRequest 对象,另一个是包含被执行X-JSON响应HTTP头。
onSuccess Function(XMLHttpRequest, Object) undefined 当AJAX请求成功完成的时候调用的自定义方法。 这个方法将被传入一个参数, 这个参数是承载AJAX操作的 XMLHttpRequest 对象,另一个是包含被执行X-JSON响应HTTP头。
onFailure Function(XMLHttpRequest, Object) undefined 当AJAX请求完成但出现错误的时候调用的自定义方法。这个方法将被传入一个参数, 这个参数是承载AJAX操作的 XMLHttpRequest 对象,另一个是包含被执行X-JSON响应HTTP头。
onException Function(Ajax.Request, exception) undefined 当一个在客户端执行的Ajax发生像无效响应或无效参数这样的异常情况时被调用的自定义函数。它收到两个参数,包含异常Ajax操作的Ajax.Request对象和异常对象。
insertion an Insertion class undefined 一个能决定怎么样插入新内容的类,能 Insertion.Before, Insertion.Top, Insertion.Bottom, 或 Insertion.After. 只能应用于Ajax.Updater 对象.

evalScripts Boolean undefined, false 决定当响应到达的时候是否执行其中的脚本块,只在 Ajax.Updater 对象中应用。
decay Number undefined, 1 决定当最后一次响应和前一次响应相同时在 Ajax.PeriodicalUpdater 对象中的减漫访问的次数, 例如,如果设为2,后来的刷新和之前的结果一样, 这个对象将等待2个设定的时间间隔进行下一次刷新, 如果又一次一样, 那么将等待4次,等等。 不设定这个只,或者设置为1,将避免访问频率变慢。
frequency Number undefined, 2 用秒表示的刷新间的间隔,只能应用于 Ajax.PeriodicalUpdater 对象。

The Ajax.Updater class
继承自 Ajax.Request
当请求的url返回一段HTML而你想把它直接放置到页面中一个特定的元素的时候被用到。 如果url的返回<script> 的块并且想在接收到时就执行它的时候也可以使用该对象。含有脚本的时候使用 evalScripts 选项。
Property Type Kind Description
containers Object instance 这个对象包含两个属性:AJAX请求成功执行的时候用到 containers.success , 否则的话用到 containers.failure 。
Method Kind Arguments Description
[ctor](container, url, options) constructor container:this can be the id of an element, the element object itself, or an object with two properties - object.success element (or id) that will be used when the AJAX call succeeds, and object.failure element (or id) that will be used otherwise. url: the url to be fetched, options: AJAX options
创建一个用给定的选项请求给定的url的一个实例。
updateContent() instance (none) 这个方法通常不会被外部调用。 当响应到达的时候,被这个对象自己调用。 它会用HTML更新适当的元素或者调用在 insertion 选项中传入的方法-这个方法将被传入两个参数, 被更新的元素和响应文本。
The Ajax.PeriodicalUpdater class
继承自Ajax.Base
这个类重复生成并使用 Ajax.Updater 对象来刷新页面中的一个元素。或者执行 Ajax.Updater 可以执行的其它任务。更多信息参照 Ajax.Updater 参考 。
Property Type Kind Description
container Object instance 这个值将直接传入Ajax.Updater的构造方法。
url String instance 这个值将直接传入Ajax.Updater的构造方法。
frequency Number instance 两次刷新之间的间隔 (不是频率) ,以秒为单位。 默认2秒。 This 当调用 Ajax.Updater 对象的时候,这个数将和当前的 decay 相乘。
decay Number instance 重负执行任务的时候保持的衰败水平。
updater Ajax.Updater
instance 最后一次使用的 Ajax.Updater 对象
timer Object instance 通知对象该下一次更新时用到的JavaScript 计时器。
Method Kind Arguments Description
[ctor](container, url, options) constructor container:this can be the id of an element, the element object itself, or an object with two properties - object.success element (or id) that will be used when the AJAX call succeeds, and object.failure element (or id) that will be used otherwise. url: the url to be fetched, options: AJAX options
创建一个用给定的选项请求给定的url的一个实例。
start() instance (none) 这个方法通常不会被外部调用。 对象为了开始周期性执行任务的时候调用的方法。
stop() instance (none) 使对象停止执行周期任务。停止后,如果有onComplete选项,那么引发callback。
updateComplete() instance (none) 这个方法通常不会被外部调用。 被当前的 Ajax.Updater 使用,当一次请求结束的时候,它被用作计划下一次请求。
onTimerEvent() instance (none) 这个方法通常不会被外部调用。当到下一次更新时被内部调用。
The Element object
这个对象提供在操作DOM中元素时使用的功能性方法。
Method Kind Arguments Description
addClassName(element, className) instance element: element object or id, className: name of a CSS class 将给出的className添加到对象的className属性中。
classNames(element) instance element: element object or id 返回一个Element.ClassName的对象表示CSS 给出对象有的class names。
cleanWhitespace(element) instance element: element object or id 清除对象子元素中所有空白的text node。
empty(element) instance element: element object or id 返回一个布尔值指示对象为空或只有空白字符。
getDimensions(element) instance element: element object or id 返回对象的尺寸,返回值有两个属性,height和width。
getHeight(element) instance element: element object or id 返回元素的 offsetHeight 。
getStyle(element, cssProperty) instance element: element object or id, cssProperty name of a CSS property (either format 'prop-name' or 'propName' works). 返回给定对象的CSS属性值或没有指定cssProperty时返回null。
hasClassName(element, className) instance element: element object or id, className: name of a CSS class 返回 true 如果元素的类名中含有给定的类名
hide(elem1 [, elem2 [, elem3 [...]]]) instance elemN: element object or id 通过设定style.display 为 'none'来隐藏每个传入的元素。
makeClipping(element) instance element: element object or id 能过设定overflow的值设定内容溢出剪辑。
makePositioned(element) instance element: element object or id 更改对象的style.position为'relative'。
remove(element) instance element: element object or id 从document对象中删除指定的元素。
removeClassName(element, className) instance element: element object or id, className: name of a CSS class 从元素的类名中删除给定的类名。
scrollTo(element) instance element: element object or id 滚动window到对象的位置。
setStyle(element, cssPropertyHash) instance element: element object or id, cssPropertyHash Hash object with the styles to be applied. 依照cssPropertyHash参数给对象设置CSS属性值。
show(elem1 [, elem2 [, elem3 [...]]]) instance elemN: element object or id 用设定它的 style.display 为 ''来显示每个传入的元素。
toggle(elem1 [, elem2 [, elem3 [...]]]) instance elemN: element object or id 切换每一个传入元素的可视性。
undoClipping(element) instance element: element object or id style.overflow的值返回上一个设定值。
undoPositioned(element) instance element: element object or id 清除对象的 style.position 为 ''
update(element, html) instance element: element object or id, html: html content 用给出的HTML参数替换对象的innerHTML,如果HTML参数中包含<script>,那么它们不会被包含进去,但是会执行。
visible(element) instance element: element object or id 返回一个布尔值指示对象可不可见。
The Element.ClassNames class
继承自 Enumerable
在一个对象中表示CSS class names的集合。
Method Kind Arguments Description
[ctor](element) constructor element: any DOM element object or id 创建一个对象,给出对象的CSS class names被表现在这个ClassNames对象中。
add(className) instance className: a CSS class name 把CSS class name包含进对象的class names 列表。
remove(className) instance className: a CSS class name 从对象的class names列表中移除className
set(className) instance className: a CSS class name 设定对象CSS class name为className,移除其它class names。
The Abstract object
这个对象是这个程序包中其他类的根。它没有任何属性和方法。在这个对象中定义的类可以被视为传统的抽象类。
The Abstract.Insertion class
这个类被用作其他提供动态内容插入功能的类的基类,它像一个抽象类一样被使用。
Method Kind Arguments Description
[ctor](element, content) constructor element: element object or id, content: HTML to be inserted 创建一个可以帮助插入动态内容的对象。
contentFromAnonymousTable() instance (none) 对content通过匿名表格变成一个Node数组。
Property Type Kind Description
adjacency String static, parameter 这个参数指定相对于给定元素,内容将被放置的位置。 可能的值是: 'beforeBegin', 'afterBegin', 'beforeEnd', 和 'afterEnd'.
element Object instance 与插入物做参照元素对象。
content String instance 被插入的 HTML 。
The Insertion object
这个对象是其他类似功能的根。它没有任何属性和方法。在这个对象中定义的类仍然可以被视为传统的抽象类。
The Insertion.Before class
继承自 Abstract.Insertion
在给定元素开始标记的前面插入HTML。
Method Kind Arguments Description
[ctor](element, content) constructor element: element object or id, content: HTML to be inserted 继承自 Abstract.Insertion. 创建一个可以帮助插入动态内容的对象。
下面的代码

<br>Hello, <span id="person" style="color:red;">Wiggum. How's it going?</span>
<script> new Insertion.Before('person', 'Chief '); </script>

将把 HTML 变为

<br>Hello, Chief <span id="person" style="color:red;">Wiggum. How's it going?</span>

The Insertion.Top class
继承自 Abstract.Insertion
在给定元素第一个子节点位置插入 HTML。内容将位于元素的开始标记的紧后面。
Method Kind Arguments Description
[ctor](element, content) constructor element: element object or id, content: HTML to be inserted 继承自 Abstract.Insertion. 创建一个可以帮助插入动态内容的对象。
下面的代码

<br>Hello, <span id="person" style="color:red;">Wiggum. How's it going?</span>
<script> new Insertion.Top('person', 'Mr. '); </script>

将把 HTML 变为

<br>Hello, <span id="person" style="color:red;">Mr. Wiggum. How's it going?</span>

The Insertion.Bottom class
Inherits from Abstract.Insertion
在给定元素最后一个子节点位置插入 HTML。内容将位于元素的结束标记的紧前面。
Method Kind Arguments Description
[ctor](element, content) constructor element: element object or id, content: HTML to be inserted Inherited from Abstract.Insertion. Creates an object that will help with dynamic content insertion.
The following code
<br>Hello, <span id="person" style="color:red;">Wiggum. How's it going?</span>

<script> new Insertion.Bottom('person', " What's up?"); </script>

Will change the HTML to
<br>Hello, <span id="person" style="color:red;">Wiggum. How's it going? What's up?</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值