正确使用Prototype
Part I: http://thinkweb2.com/projects/prototype-checklist/
1,错:
对:
2,错:
对:
3,错:
对:
4,错:
对:
5,错:
对:
6,错:
对:
7,错:
对:
8,错:
对:
9,错:
对:
10,错:
对:
11,错:
对:
Part II: http://thinkweb2.com/projects/prototype/?p=3
1,轻松监听键盘事件
2,不需要事件capturing
3,聪明的insert()
4,Form提交
Enjoy prototyping!
1,错:
- document.getElementById('foo')
- $('foo')
- var woot = document.getElementById('bar').value
- var woot = $('bar').value
- var woot = $F('bar')
- $('footer').style.height = '100px';
- $('footer').style.background = '#ffc';
- $('footer').setStyle({
- height: '100px',
- background: '#ffc'
- })
- $('coolestWidgetEver').innerHTML = 'some nifty content'
- $('coolestWidgetEver').update('some nifty content')
- $('coolestWidgetEver').update('some nifty content').addClassName('highlight').next().hide()
- new Ajax.Request('ninja.php?weapon1=foo&weapon2=bar')
- new Ajax.Request('ninja.php', {
- parameters: {
- weapon1: 'foo',
- weapon2: 'bar'
- }
- })
- new Ajax.Request('blah.php', {
- method: 'POST',
- asynchronous: true,
- contentType: 'application/x-www-form-urlencoded',
- encoding: 'UTF-8',
- })
- new Ajax.Request('blah.php')
- Event.observe('myContainer', 'click', doSomeMagic)
- $('myContainer').observe('click', doSomeMagic)
- $$('div.hidden').each(function(el) {
- el.show();
- })
- $$('div.hidden').invoke('show')
- $$('div.collapsed').each(function(el) {
- el.observe('click', expand);
- })
- $$('div.collapsed').invoke('observe', 'click', expand)
- $$('input.date').invoke('observe', 'focus', onFocus);
- $$('input.date').invoke('observe', 'blur', onBlur);
- $$('input.date')
- .invoke('observe', 'focus', onFocus)
- .invoke('observe', 'blur', onBlur)
- $('productTable').innerHTMl =
- $('productTable').innerHTML +
- '<tr><td>' + productId + ' '
- + productName + '</td></tr><tr><td>'
- + productId + ' ' + productPrice +
- '</td></tr>'
- 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
- }))
- )
1,轻松监听键盘事件
- $('myInput').observe('keyup', function(e) {
- if (e.keyCode == Event.KEY_TAB)
- doSomethingCoolWhenTabIsPressed();
- })
- $('productInfo').observe('click', displayProductInfo, false); // 'false' could be skipped
- $('productInfo').observe('click', displayProductInfo);
- 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
- }));
- $('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])'))
- })
- })
莫愁前路无知己,天下无人不识君。