/**
* 新增子节点
*
* @param nodParent - 父节点
* @param arrChildNode - 要新增的子节点数组
*/
function addChildNodes(nodParent, arrChildNode) {
for(var i = 0; i < arrChildNode.length; i++) {
nodParent.appendChild(arrChildNode[i]);
}
}
/**
* 根据子节点 id 删除子节点
*
* @param nodParent - 父节点
* @param arrId - 要删除的子节点 id 数组
*/
function delChildNodes(nodParent, arrId) {
var nodChild;
for(var i = 0; i < arrId.length; i++) {
nodChild = document.getElementById(arrId[i]);
if (nodChild != null) {
nodParent.removeChild(nodChild);
}
}
}
/**
* 删除所有子节点
*
* @param nodParent - 父节点
*/
function delAllChildNodes(nodParent) {
while(nodParent.hasChildNodes()) {
nodParent.removeChild(nodParent.firstChild);
}
}
/**
* 删除节点
*
* @param node - 节点
*/
function delNode(node) {
var nodParent = node.parentNode;
nodParent.removeChild(node);
}
/**
* 更新子节点
*
* @param nodParent - 父节点
* @param arrChildNode - 要更新的子结点数组
*/
function updateChildNodes(nodParent, arrChildNode) {
var nodOld, nodNew;
for(var i = 0; i < arrChildNode.length; i++) {
nodNew = arrChildNode[i];
nodOld = document.getElementById(nodNew.getAttribute("id"));
if (nodOld != null) {
nodParent.replaceChild(nodNew, nodOld);
}
}
}
/**
* 创建 a 节点
*
* @param strHref - a 的 href 属性
* @param strTxt - a 的文字显示
* @param strTitle - a 的 title 属性
*/
function createANode(strHref, strTxt, strTitle) {
var nodA = document.createElement("a");
nodA.setAttribute("href", strHref);
if (isNotBlankStr(strTitle)) {
nodA.setAttribute("title", strTitle);
}
nodA.appendChild(document.createTextNode(strTxt));
return nodA;
}
/**
* 替换节点中的文本
*
* @param nod - 节点
* @param strText - 新文本
*/
function replaceText(nod, strText) {
delAllChildNodes(nod);
nod.appendChild(document.createTextNode(strText));
}
/*--------------------------------------------------------------------------*/
/**
* 将字符串分割成字符串数组
*
* @param str - 要分割的字符串
* @param strComp - 分隔符
* @param strPre - 分割后字符串的前缀
*/
function splitStr(str, strComp, strPre) {
if (isNotBlankStr(strPre)) {
var arrTemp = str.split(strComp);
var arrNew = new Array(arrTemp.length);
for(var i = 0; i < arrTemp.length; i++) {
arrNew[i] = strPre + arrTemp[i];
}
return arrNew;
} else {
return str.split(strComp);
}
}
/**
* 判断对象是否为非空数组
*
* @param arr - 数组对象
*/
function isNotEmptyArray(arr) {
if ((arr == null) || (arr.length == null) || (arr.length < 1)) {
return false;
}
return true;
}
/**
* 判断字符串是否为空串
*
* @param str - 字符串
*/
function isNotBlankStr(str) {
if ((str == null) || (str == "")) {
return false;
}
return true;
}
/**
* trim字符串
*
* @param str - 字符串
*/
function trim(str) {
if (!isNotBlankStr(str)) {
return str;
}
return str.replace(/(^\s*)|(\s*$)/g, "");
}
/**
* 将字符串转换成指定长度的缩略
*
* @param str - 字符串
* @param maxWidth - 最大长度
*/
function abbreviate(str, maxWidth) {
if (!isNotBlankStr(str)) {
return str;
}
if (maxWidth < 4) {
maxWidth = 4;
}
if (str.length <= maxWidth) {
return str;
}
return str.substr(0, maxWidth - 3) + "...";
}
/**
* 将DIV的滚动条滚到最后
*/
function scrollDiv(objDiv) {
objDiv.scrollTop = 100000;
}
/*--------------------------------------------------------------------------*/
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;
}
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 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;
}
