惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
Recorded Future
Recorded Future
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
Y
Y Combinator Blog
N
News | PayPal Newsroom
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Help Net Security
Help Net Security
博客园 - Franky
SecWiki News
SecWiki News
Recent Announcements
Recent Announcements
T
Troy Hunt's Blog
The Register - Security
The Register - Security
The Last Watchdog
The Last Watchdog
Webroot Blog
Webroot Blog
S
Security Affairs
博客园 - 司徒正美
S
Schneier on Security
I
InfoQ
博客园_首页
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Threat Research - Cisco Blogs
Forbes - Security
Forbes - Security
腾讯CDC
N
Netflix TechBlog - Medium
N
News and Events Feed by Topic
Cloudbric
Cloudbric
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
Recent Commits to openclaw:main
Recent Commits to openclaw:main
B
Blog
V
Vulnerabilities – Threatpost
C
Check Point Blog
Google DeepMind News
Google DeepMind News
Google Online Security Blog
Google Online Security Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cisco Blogs
Schneier on Security
Schneier on Security
O
OpenAI News
K
Kaspersky official blog

博客园 - laolaowhn

Clean code that works (simplicity is more complicity than you think 朴素比你想象的更复杂) jQuery in Action 中文版 第一章 jQuery简介 如翻译的不好请原谅 jQuery in Action 中文版 简介 专用卸载工具 Windows Installer Clean Up Uninstall 临时表 中文转拼音源代码 ([^<]*) - laolaowhn - 博客园 IIS6架设网站常见问题及症状举例答疑 - laolaowhn - 博客园 jquery验证表单,可以独立与文框,利jquery动态控制,源代码 vs2005 js 没有反应 - laolaowhn {JS}JavaScript使用正则表达式 JavaScript 正则表达式 验证网址 JavaScript下正则表达式密码强度验证 悟透JavaScript引子 prototype学习心得 prototype源码分析—enumerable Prototype 框架分析(一) 我为什么选择mootools,抛弃了prototype. (mootools 与 prototype 核心代码分析) prototype源码分析
Prototype.js的分析
laolaowhn · 2008-02-23 · via 博客园 - laolaowhn

Prototype.js的分析

2007-11-16 17:13

 

prototype.js是 一个很强大的Javascript函数库,它可以让你很轻松的使用一些特效,实现AJAX的功能.虽然prototype.js是为了Ruby On Rails开发的,它的纯Javascript的性质也使得它很容易用在其他的网络程序中。可惜的是,Prototype.js还没有强大的文档解释,尽 管它的代码非常有条理,但是也给初学者造成了一定的麻烦。作者在README里说:

Prototype is embarrassingly lacking in documentation。(The source code should be fairly easy to comprehend;I’m committed to using a clean style with meaningful identifiers。But I know that only goes so far.)

经过这两天的学习,我总结了一下Prototype的主要用法:

基本用法:

  以Element Class为例,prototype给每个主要的分类都分成了一个Class,使用起来很方便,要产生特定的效果的话只要用new Class.function(<argument>)就可以了。
比如:

<DIV id="div1">
<a href="#" onclick="new Element.toggle(''div2'')">
Click Me
</a>
</DIV>

<DIV id="div2">Hello!</DIV>

当点击Click Me的时候,div2就会交替隐藏或显示。注意,你可以给toggle加上无限个parameter,比如Element.toggle(''div2'',''div3'',''div4'')...

下面是每个重要的类和函数的解释:

prototype 1.2.0 的函数简介
函数名 解释 举例
Element.toggle 交替隐藏或显示 "Element.toggle(''div1'',''div2'')
Element.hide 隐藏 "Element.hide(''div1'',''div2'')
Element.show 显示 "Element.show(''div1'',''div2'')
Element.remove 删除 "Element.remove(''div1'',''div2'')
Element.getHeight 取得高度 "Element.getHeight(''div1'')
Toggle.display 和Element.toggle相同 "Toggle.display(''div1'',''div2'')
Insertion.Before 在DIV前插入文字 "Insertion.Before(''div1'',''my content'')
Insertion.After 在DIV后插入文字 "Insertion.After(''div1'',''my content'')
Insertion.Top 在DIV里最前插入文字 "Insertion.Top(''div1'',''this is a text'')
Insertion.Bottom 在DIV里最后插入文字 "Insertion.Bottom(''div1'',''this is a text'')
PeriodicalExecuter 以给定频率调用一段JavaScript "PeridicalExecutor(test, 1)"这里test是Javascript的函数,1是频率(1秒).
$ 取得一个DIV, 相当于getElementById() $(''div1'')
Field.clear 清空一个输入框 "Field.clear(''textfield1'')
Field.focus 把 焦点集中在输入框上 "Field.focus(''select1'')
Field.present 判断内容是否为空 "alert(Field.present(''textfield1''))"
Field.select 选择输入框的内容 "Field.select(''textfield1'')"
Field.activate 把 焦点集中在输入框上并选择输入框的内容 "Field.activate(''textfield1'')"
Form.serialize 把表格内容转化成string
Form.getElements 取得表格内容为数组形式
Form.disable disable表格所有内容 Form.disable(''form1'') (这个好象不work)
Form.focusFirstElement 把焦点集中在表格第一个元素上 Form.focusFirstElement(''form1'')
Form.reset Reset表格 Form.reset(''form1'')
Form.Element.getValue 取得表格输入框的值 Form.Element.getValue(''text1'')
Form.Element.serialize    把表格中输入框内容转化成string Form.Element.serialize(''text1'')
$F 等同于Form.Element.getValue() $F(''text1'')
Effect.Highlight 高亮特效. Effect.Highlight(''text1'')
Effect.Fade 褪色特效
Effect.Scale 放大缩小(百分比)

Effect.Scale(''text1'', 200)
这里200 = 200%, 即两倍

Effect.Squish 消失特效.文字缩小后消失 Effect.Squish(''text1'')
Effect.Puff 消失特效.文字放大后消失 Effect.Puff(''text1'')
Effect.Appear 出现特效
Effect.ContentZoom ZOOM特效.
Ajax.Request 传送Ajax请求给服务器 Ajax.Request(''http://server/s.php'')
Ajax.Updater 传送Ajax请求给服务器并用答复的结果更新指定的Container Ajax.Updater(''text1'',''http://server/s.php'')

Ajax的函数实际上还有一个可选参数,就是options.在未指明的情况下,Ajax使用的是''POST''发送请求,而且是异步执行,如果 想要改用''GET''和同步,就可以用Ajax.Request(''http://server/s.php'',''get'','''',''a =1&b=2'')来执行.

在Rails中Ajax的函数被封装成Ruby的函数,所以不必直接采用Ajax.Request,Ajax.Updater。但是知道它是怎么工作的也很有用。