JavaScript 测试 Prototype

Prototype是一个JavaScript库,提供了简化DOM操作的函数。$()类似于jQuery的选择器,但Prototype不使用ready()方法,而是通过Event.observe()处理窗口加载事件。这两个示例演示了如何使用Prototype改变HTML元素的内容和样式,并展示了链式语法的使用。

测试 JavaScript 框架库 - Prototype


引用 Prototype

如需测试 JavaScript 库,您需要在网页中引用它。

为了引用某个库,请使用 <script> 标签,其 src 属性设置为库的 URL:

引用 Prototype

<!DOCTYPE html>

<html>

<head>

<script src="http://apps.bdimg.com/libs/prototype/1.7.1.0/prototype.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>

</head>

<body>

</body>

</html>


Prototype 描述

Prototype 提供的函数可使 HTML DOM 编程更容易。

与 jQuery 类似,Prototype 也有自己的 $() 函数。

$() 函数接受 HTML DOM 元素的 id 值(或 DOM 元素),并会向 DOM 对象添加新的功能。

与 jQuery 不同,Prototype 没有用以取代 window.onload() 的 ready() 方法。相反,Prototype 会向浏览器及 HTML DOM 添加扩展。

在 JavaScript 中,您可以分配一个函数以处理窗口加载事件:

JavaScript 方式:

function myFunction(){

var obj=document.getElementById("h01");

obj.innerHTML="Hello Prototype";

}

onload=myFunction;

等价的 Prototype 是不同的:

Prototype 方式:

function myFunction(){

$("h01").insert("Hello Prototype!");

}

Event.observe(window,"load",myFunction);

Event.observe() 接受三个参数:

  • 您希望处理的 HTML DOM 或 BOM(浏览器对象模型)对象

  • 您希望处理的事件

  • 您希望调用的函数


测试 Prototype

请试一下下面这个例子:

Example

<!DOCTYPE html>

<html>

<script src="http://apps.bdimg.com/libs/prototype/1.7.1.0/prototype.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>

<script>

function myFunction(){

$("h01").insert("Hello Prototype!");

}

Event.observe(window,"load",myFunction);

</script>

</head>

<body>

<h1 id="h01"></h1>

</body>

</html>

尝试一下 »

请再试一下这个例子:

Example

<!DOCTYPE html>

<html>

<script src="http://apps.bdimg.com/libs/prototype/1.7.1.0/prototype.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>

<script>

function myFunction(){

$("h01").writeAttribute("style","color:red").insert("Hello Prototype!");

}

Event.observe(window,"load",myFunction);

</script>

</head>

<body>

<h1 id="h01"></h1>

</body>

</html>

测试一下 »

正如您在上面的例子中看到的,与 jQuery 相同,Prototype 允许链式语法。

链接(Chaining)是一种在同一对象上执行多个任务的便捷方法。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值