当前位置: 首页 > 图文教程 > 网络编程 > Javascript > Javascript YUI 读码日记之 YAHOO.util.Dom - Part.3

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript YUI 读码日记之 YAHOO.util.Dom - Part.3


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 162 ::
收藏到网摘: n/a

在 YAHOO.util.Dom 中能发现很多有趣的东西。下面先说下 toCamel 的函数,感谢 小马 帮助我理解了这个函数。toCamel 把指定名称替换为驼峰写法,比如把 border-width 替换为 borderWidth 。 var patterns = {
HYPHEN: /(-[a-z])/i,
ROOT_TAG: /^body|html$/i
};
var toCamel = function(property) {
// 如果没有 -[a-z] 字母,则直接返回
if ( !patterns.HYPHEN.test(property) ) {
return property;
}
// 如果有缓存,直接返回替换后的值
if (propertyCache[property]) {
return propertyCache[property];
}
// 使用正则替换
var converted = property;
while( patterns.HYPHEN.exec(converted) ) {
converted = converted.replace(RegExp.$1,
RegExp.$1.substr(1).toUpperCase());
}
// 存入缓存
propertyCache[property] = converted;
return converted;
};在 YAHOO.util.Dom 中,getStyle 函数考虑了更多不同浏览器兼容性方面的问题,代码如下
// 使用 W3C DOM 标准的浏览器,比如 Firefox、Opera、Safari
if (document.defaultView && document.defaultView.getComputedStyle) {
getStyle = function(el, property) {
var value = null;
// 重命名部分 CSS 样式名
if (property == 'float') {
property = 'cssFloat';
}
// 获取通过 CSS 加上去的属性
var computed = document.defaultView.getComputedStyle(el, '');
if (computed) {
value = computed[toCamel(property)];
}
return el.style[property] || value;
};
// 如果是 IE 浏览器
} else if (document.documentElement.currentStyle && isIE) {
getStyle = function(el, property) {
switch( toCamel(property) ) {
// “转换”名称为 IE 可以认识的
case 'opacity' :
var val = 100;
try {
val =
el.filters['DXImageTransform.Microsoft.Alpha'].opacity;
} catch(e) {
try {
val = el.filters('alpha').opacity;
} catch(e) {
}
}
// 百分比
return val / 100;
case 'float':
property = 'styleFloat';
default:
var value = el.currentStyle ? el.currentStyle[property] : null;
return ( el.style[property] || value );
}
};
} else {
// 获取内联样式
getStyle = function(el, property) { return el.style[property]; };
}另外,PPK 在他的 Blog 上的有关 getStyle 的阐述,也很精彩,有兴趣的可以去看下。