正确使用Prototype,节省额外的100K

本文列举了使用Prototype.js库进行高效Web开发的多种技巧,包括简化DOM操作、事件监听及Ajax请求等,帮助开发者提高生产力。
Part I: [url]http://thinkweb2.com/projects/prototype-checklist/[/url]
1,错:
[code]
document.getElementById('foo')
[/code]
对:
[code]
$('foo')
[/code]

2,错:
[code]
var woot = document.getElementById('bar').value
var woot = $('bar').value
[/code]
对:
[code]
var woot = $F('bar')
[/code]

3,错:
[code]
$('footer').style.height = '100px';
$('footer').style.background = '#ffc';
[/code]
对:
[code]
$('footer').setStyle({
height: '100px',
background: '#ffc'
})
[/code]

4,错:
[code]
$('coolestWidgetEver').innerHTML = 'some nifty content'
[/code]
对:
[code]
$('coolestWidgetEver').update('some nifty content')
$('coolestWidgetEver').update('some nifty content').addClassName('highlight').next().hide()
[/code]

5,错:
[code]
new Ajax.Request('ninja.php?weapon1=foo&weapon2=bar')
[/code]
对:
[code]
new Ajax.Request('ninja.php', {
parameters: {
weapon1: 'foo',
weapon2: 'bar'
}
})
[/code]

6,错:
[code]
new Ajax.Request('blah.php', {
method: 'POST',
asynchronous: true,
contentType: 'application/x-www-form-urlencoded',
encoding: 'UTF-8',
})
[/code]
对:
[code]
new Ajax.Request('blah.php')
[/code]

7,错:
[code]
Event.observe('myContainer', 'click', doSomeMagic)
[/code]
对:
[code]
$('myContainer').observe('click', doSomeMagic)
[/code]

8,错:
[code]
$$('div.hidden').each(function(el) {
el.show();
})
[/code]
对:
[code]
$$('div.hidden').invoke('show')
[/code]

9,错:
[code]
$$('div.collapsed').each(function(el) {
el.observe('click', expand);
})
[/code]
对:
[code]
$$('div.collapsed').invoke('observe', 'click', expand)
[/code]

10,错:
[code]
$$('input.date').invoke('observe', 'focus', onFocus);
$$('input.date').invoke('observe', 'blur', onBlur);
[/code]
对:
[code]
$$('input.date')
.invoke('observe', 'focus', onFocus)
.invoke('observe', 'blur', onBlur)
[/code]

11,错:
[code]
$('productTable').innerHTMl =
$('productTable').innerHTML +
'<tr><td>' + productId + ' '
+ productName + '</td></tr><tr><td>'
+ productId + ' ' + productPrice +
'</td></tr>'
[/code]
对:
[code]
var rowTemplate = new Template('<tr><td>#{id} #{name</td></tr><tr><td>#{id} #{price}</td></tr>');
$('productTable').insert(
rowTemplate.evaluate({
id: productId,
name: productName,
price: productPrice
}))
)
[/code]

Part II: [url]http://thinkweb2.com/projects/prototype/?p=3[/url]
1,轻松监听键盘事件
[code]
$('myInput').observe('keyup', function(e) {
if (e.keyCode == Event.KEY_TAB)
doSomethingCoolWhenTabIsPressed();
})
[/code]

2,不需要事件capturing
[code]
$('productInfo').observe('click', displayProductInfo, false); // 'false' could be skipped
$('productInfo').observe('click', displayProductInfo);
[/code]

3,聪明的insert()
[code]
new Insertion.Bottom('blogEntry',
new Template('<div><h2>#{name}</h2><p>#{content}</p></div>')
.evaluate({
name: blogEntry.name,
content: blogEntry.content
})
);
// Insertion class is deprecated - it's recommended to use Element's insert method:

$('blogEntry').insert(new Template('<div><h2>#{name}</h2><p>#{content}</p></div>')
.evaluate({
name: blogEntry.name,
content: blogEntry.content
}), 'bottom'); // 'bottom' can be skipped

$('blogEntry').insert(new Template('<div><h2>#{name}</h2><p>#{content}</p></div>')
.evaluate{(
name: blogEntry.name,
content: blogEntry.content
}));
[/code]

4,Form提交
[code]
$('register').observe('submit', function(e) {
Event.stop(e);
$(this).request();
})

$('register').observe('submit', function(e) {
Event.stop(e);
new Ajax.Request($(this).readAttribute('action', {
parameters: Form.serializeElements($(this).getInputs('', 'email'))
})
})

$('register').observe('submit', function(e) {
Event.stop(e);
new Ajax.Request(this.readAttribute('action'), {
parameters: Form.serializeElements($(this).getElements()
.reject(function(el) {return el.hasAtrribute('multiple')})
);
})
})

$('register').observe('submit', function(e) {
Event.stop(e);
new Ajax.Request($(this).readAttribute('action', {
parameters: Form.serializeElements($$('#register input:not([multiple])'))
})
})
[/code]

Enjoy prototyping!
这个是完整源码 python实现 Flask,Vue 【python毕业设计】基于Python的Flask+Vue物业管理系统 源码+论文+sql脚本 完整版 数据库是mysql 本文首先实现了基于Python的Flask+Vue物业管理系统技术的发展随后依照传统的软件开发流程,最先为系统挑选适用的言语和软件开发平台,依据需求分析开展控制模块制做和数据库查询构造设计,随后依据系统整体功能模块的设计,制作系统的功能模块图、E-R图。随后,设计框架,依据设计的框架撰写编码,完成系统的每个功能模块。最终,对基本系统开展了检测,包含软件性能测试、单元测试和性能指标。测试结果表明,该系统能够实现所需的功能,运行状况尚可并无明显缺点。本文首先实现了基于Python的Flask+Vue物业管理系统技术的发展随后依照传统的软件开发流程,最先为系统挑选适用的言语和软件开发平台,依据需求分析开展控制模块制做和数据库查询构造设计,随后依据系统整体功能模块的设计,制作系统的功能模块图、E-R图。随后,设计框架,依据设计的框架撰写编码,完成系统的每个功能模块。最终,对基本系统开展了检测,包含软件性能测试、单元测试和性能指标。测试结果表明,该系统能够实现所需的功能,运行状况尚可并无明显缺点。本文首先实现了基于Python的Flask+Vue物业管理系统技术的发展随后依照传统的软件开发流程,最先为系统挑选适用的言语和软件开发平台,依据需求分析开展控制模块制做和数据库查询构造设计,随后依据系统整体功能模块的设计,制作系统的功能模块图、E-R图。随后,设计框架,依据设计的框架撰写编码,完成系统的每个功能模块。最终,对基本系统开展了检测,包含软件性能测试、单元测试和性能指标。测试结果表明,该系统能够实现所需的功能,运行状况尚可并无明显缺点。本文首先实现了基于Python的Flask+Vue物业管理系统技术的发
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值