Prototype 學(xué)習(xí) 工具函數(shù)學(xué)習(xí)($w,$F方法)
更新時(shí)間:2009年07月12日 22:12:33 作者:
Prototype $w $F使用方法
$w方法
Splits a string into an Array, treating all whitespace as delimiters. Equivalent to Ruby's %w{foo bar} or Perl's qw(foo bar).
function $w(string) {
if (!Object.isString(string)) return [];
string = string.strip();
return string ? string.split(/\s+/) : [];
}
這個(gè)方法就是用空白字符把字符串分成數(shù)組,然后返回。
例子:
$w('apples bananas kiwis') // -> ['apples', 'bananas', 'kiwis']
$F方法
Returns the value of a form control. This is a convenience alias of Form.Element.getValue.
var $F = Form.Element.Methods.getValue;
//====>getValue()
getValue: function(element) {
element = $(element);
var method = element.tagName.toLowerCase();
return Form.Element.Serializers[method](element);
}
//====>Serializers
Form.Element.Serializers = {
input: function(element, value) {
switch (element.type.toLowerCase()) {
case 'checkbox':
case 'radio':
return Form.Element.Serializers.inputSelector(element, value);
default:
return Form.Element.Serializers.textarea(element, value);
}
},
inputSelector: function(element, value) {
if (Object.isUndefined(value)) return element.checked ? element.value :
null;
else element.checked = !!value;
},
textarea: function(element, value) {
if (Object.isUndefined(value)) return element.value;
else element.value = value;
},
//省略,以后說到這個(gè)對(duì)象的時(shí)候在詳細(xì)說明
......
//====> Object.isUndefined
function isUndefined(object) {
return typeof object === "undefined";
}
這個(gè)函數(shù)最后就是返回傳入?yún)?shù)的值。從Form.Element.Serializers 這個(gè)對(duì)象里面定義的方法可以看出,$F方法取得的是Form元素的值,如果定義一個(gè)div然后調(diào)用這個(gè)方法將會(huì)拋出Form.Element.Serializers[method] is not a function異常,如果給定的ID不存在將會(huì)拋出element has no properties異常。
在Form.Element.Serializers 里面的方法中先檢查value這個(gè)參數(shù)是否存在,存在就相當(dāng)于給element參數(shù)賦值,不存在就會(huì)返回element的值
Splits a string into an Array, treating all whitespace as delimiters. Equivalent to Ruby's %w{foo bar} or Perl's qw(foo bar).
復(fù)制代碼 代碼如下:
function $w(string) {
if (!Object.isString(string)) return [];
string = string.strip();
return string ? string.split(/\s+/) : [];
}
這個(gè)方法就是用空白字符把字符串分成數(shù)組,然后返回。
例子:
復(fù)制代碼 代碼如下:
$w('apples bananas kiwis') // -> ['apples', 'bananas', 'kiwis']
$F方法
Returns the value of a form control. This is a convenience alias of Form.Element.getValue.
復(fù)制代碼 代碼如下:
var $F = Form.Element.Methods.getValue;
//====>getValue()
getValue: function(element) {
element = $(element);
var method = element.tagName.toLowerCase();
return Form.Element.Serializers[method](element);
}
//====>Serializers
Form.Element.Serializers = {
input: function(element, value) {
switch (element.type.toLowerCase()) {
case 'checkbox':
case 'radio':
return Form.Element.Serializers.inputSelector(element, value);
default:
return Form.Element.Serializers.textarea(element, value);
}
},
inputSelector: function(element, value) {
if (Object.isUndefined(value)) return element.checked ? element.value :
null;
else element.checked = !!value;
},
textarea: function(element, value) {
if (Object.isUndefined(value)) return element.value;
else element.value = value;
},
//省略,以后說到這個(gè)對(duì)象的時(shí)候在詳細(xì)說明
......
//====> Object.isUndefined
function isUndefined(object) {
return typeof object === "undefined";
}
這個(gè)函數(shù)最后就是返回傳入?yún)?shù)的值。從Form.Element.Serializers 這個(gè)對(duì)象里面定義的方法可以看出,$F方法取得的是Form元素的值,如果定義一個(gè)div然后調(diào)用這個(gè)方法將會(huì)拋出Form.Element.Serializers[method] is not a function異常,如果給定的ID不存在將會(huì)拋出element has no properties異常。
在Form.Element.Serializers 里面的方法中先檢查value這個(gè)參數(shù)是否存在,存在就相當(dāng)于給element參數(shù)賦值,不存在就會(huì)返回element的值
相關(guān)文章
Prototype 學(xué)習(xí) Prototype對(duì)象
Prototype 學(xué)習(xí) Prototype對(duì)象2009-07-07Prototype源碼淺析 Enumerable部分之each方法
在javascript中,根本找不到Enumerable的影子,因?yàn)檫@一塊是Prototype作者從Ruby中借鑒過來的。2012-01-01Prototype String對(duì)象 學(xué)習(xí)
這個(gè)對(duì)象里面的方法就是提供了一些字符串操作的工具方法,比較重要的gsub方法,下面做了詳細(xì)的注釋,簡(jiǎn)單的方法就不說了,一看就明白了。2009-07-07用prototype實(shí)現(xiàn)的簡(jiǎn)單小巧的多級(jí)聯(lián)動(dòng)菜單
用prototype實(shí)現(xiàn)的簡(jiǎn)單小巧的多級(jí)聯(lián)動(dòng)菜單...2007-03-03初學(xué)prototype,發(fā)個(gè)JS接受URL參數(shù)的代碼
初學(xué)prototype,發(fā)個(gè)JS接受URL參數(shù)的代碼...2006-09-09