//浏览器识别
var TheBrowser = {
'isIE' : (navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0),
'isIE6' : (document.all && !window.XMLHttpRequest) ,
'isIE7' : (document.all && window.XMLHttpRequest) ,
'isFirefox' : navigator.userAgent.indexOf('Firefox') >= 0,
'isOpera' : navigator.userAgent.indexOf('Opera') >= 0
};
function $(s){
return document.getElementById(s);
}
//建立事件绑定
function addEvent(el,eventType,fn){
if(el.addEventListener){
el.addEventListener(eventType,fn,false);
}else if(el.attachEvent){
el.attachEvent("on" + eventType,fn);
}else{
el["on"+eventType]=fn;
}
}
function trim(inputString) {
return inputString.replace(/^[\s]+/,"").replace(/[\s]+$/,"");
}
function drawImageByStandard(im,x,y) {
y = y || 99999;
im.removeAttribute("width");
im.removeAttribute("height");
if( im.width/im.height > x/y  && im.width >x ){
im.height = im.height * (x/im.width)
im.width = x
im.parentNode.style.height = im.height * (x/im.width) + 'px'
}else if( im.width/im.height <= x/y && im.height >y){
im.width = im.width * (y/im.height)
im.height = y
im.parentNode.style.height = y + 'px'
}
im.style.visibility = 'visible'
}
//获取屏幕和页面的宽高，并以数组形式返回
function getSize() {
var xScroll, yScroll;
if (window.innerHeight && window.scrollMaxY) {
xScroll = document.body.scrollWidth;
yScroll = window.innerHeight + window.scrollMaxY;
} else if (document.body.scrollHeight > document.body.offsetHeight){      // all but Explorer Mac
xScroll = document.body.scrollWidth;
yScroll = document.body.scrollHeight;
} else {      // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
xScroll = document.body.offsetWidth;
yScroll = document.body.offsetHeight;
}
var windowWidth, windowHeight;
if (self.innerHeight) {      // all except Explorer
windowWidth = self.innerWidth;
windowHeight = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) {      // Explorer 6 Strict Mode
windowWidth = document.documentElement.clientWidth;
windowHeight = document.documentElement.clientHeight;
} else if (document.body) {      // other Explorers
windowWidth = document.body.clientWidth;
windowHeight = document.body.clientHeight;
}
// for small pages with total height less then height of the viewport
if(yScroll < windowHeight){
pageHeight = windowHeight;
y = pageHeight;
} else {
pageHeight = yScroll;
y = pageHeight;
}
if(xScroll < windowWidth){
pageWidth = windowWidth;
} else {
pageWidth = xScroll;
}
arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
return arrayPageSize;
}
//重写insertAdjacentHTML函数 开始
if (!document.all) {
HTMLElement.prototype.insertAdjacentHTML = function (sWhere, sHTML) {
var df;
var r = this.ownerDocument.createRange();
switch (String(sWhere).toLowerCase()) {
case "beforebegin":
r.setStartBefore(this);
df = r.createContextualFragment(sHTML);
this.parentNode.insertBefore(df, this);
break;
case "afterbegin":
r.selectNodeContents(this);
r.collapse(true);
df = r.createContextualFragment(sHTML);
this.insertBefore(df, this.firstChild);
break;
case "beforeend":
r.selectNodeContents(this);
r.collapse(false);
df = r.createContextualFragment(sHTML);
this.appendChild(df);
break;
case "afterend":
r.setStartAfter(this);
df = r.createContextualFragment(sHTML);
this.parentNode.insertBefore(df, this.nextSibling);
break;
}
};
}
//重写insertAdjacentHTML函数 结束
//事件冒泡终止函数
function cancelBubble(e){
e = e || window.event;e.cancelBubble=true
}
//获取当前的样式属性值
function getCurrentStyle(obj, prop) {
if (obj.currentStyle) {
return obj.currentStyle[prop];
}
else if (window.getComputedStyle) {
prop = prop.replace (/([A-Z])/g, "-$1");
prop = prop.toLowerCase ();
return window.getComputedStyle (obj, "").getPropertyValue(prop);
}
return null;
}
//通过classname获取节点
function getElementsByClassName(cls,elm,pobj) {
cls = cls.replace(',','\\b.*\\b')
var arrCls =[];
var rexCls = new RegExp('\\b' + cls + '\\b','img');
var lisElm = pobj.getElementsByTagName(elm);
for (var i=0; i<lisElm.length; i++ ){
var evaCls = lisElm[i].className;
if( rexCls.test(evaCls) ){
arrCls.push(lisElm[i]);
rexCls.test(evaCls)
}
}
return arrCls;
}
/*
*函数名：getPreviousSibling:    获取上一个节点
*@param o:              参数节点；
*@return: 参数节点的上一个节点，如果为空返回 null；
*/
function getPreviousSibling(o){
var r = o.previousSibling
while( r && r.nodeType !=1 ){
r = r.previousSibling
}
return r
}
/*
*函数名：getNextSibling:    获取下一个节点
*@param o:              参数节点；
*@return: 参数节点的下一个节点，如果为空返回 null；
*/
function getNextSibling(o){
var r = o.nextSibling
while( r && r.nodeType !=1 ){
r = r.nextSibling
}
return r
}
/*
获得指定className的最近上层
*/
function getParentByClassName(classname,obj){
classname = classname.replace(',','\\b.*\\b')
var rg = new RegExp('\\b'+classname+'\\b','ig')
var pn = obj.parentNode;
while( !rg.test(pn.className) ){
var tg = pn.tagName ? pn.tagName.toLowerCase() : ''
if( tg == 'body' ||  tg == '' || pn.parentNode && !pn.parentNode.tagName ){
return null;
}
pn = pn.parentNode;
}
return pn;
}
/*
*函数名：getLastElementChild:    获取父级元素的最后一个element子元素；
*@param obj:    父级元素；
*@type Object
*@return: 最后一个类型为element的子元素；
*/
function getLastElementChild(obj){
var objChilds /*父元素的所有子元素*/ = obj.childNodes;
for(var i=objChilds.length-1;i>=0;i--){
if(objChilds[i].nodeType == 1){
return objChilds[i];
break;
}
}
return null;
}
/*
*函数名：getFirstElementChild:   获取父级元素的第一个element子元素；
*@param obj:    父级元素；
*@type Object
*@return: 第一个类型为element的子元素；
*/
function getFirstElementChild(obj){
var objChilds /*父元素的所有子元素*/ = obj.childNodes;
for(var i=0;i<objChilds.length;i++){
if(objChilds[i].nodeType == 1){
return objChilds[i];
break;
}
}
return null;
}
/*
获得指定className的最近上层
*/
function getParentByClassName(classname,obj){
classname = classname.replace(',','\\b.*\\b')
var rg = new RegExp('\\b'+classname+'\\b','ig')
var pn = obj.parentNode;
while( !rg.test(pn.className) ){
var tg = pn.tagName ? pn.tagName.toLowerCase() : ''
if( tg == 'body' ||  tg == '' || pn.parentNode && !pn.parentNode.tagName ){
return null;
}
pn = pn.parentNode;
}
return pn;
}
