标签:
itcast.fn.extend({
css: function(name, value) {
return window.getComputedStyle(this[0])[name];
}
});
itcast.fn.extend({
css: function(name, value) {
if(value === undefined) {
return window.getComputedStyle(this[0])[name];
}
return this.each(function() {
this.style[name] = value;
});
}
});
itcast.fn.extend({
css: function(name, value) {
if(value === undefined) {
if(typeof name === "object") {
return this.each(function() {
for(var k in name) {
this.style[k] = name[k];
}
});
}
return window.getComputedStyle(this[0])[name];
} else {
return this.each(function() {
this.style[name] = value;
});
}
}
});
itcast.fn.extend({
hasClass: function(cName) {
var hasCName = false;
// 判断第一个元素
itcast.each(this[0].className.split(" "), function(i, v) {
if(v === cName) {
hasCName = true;
return false;
}
});
return hasCName;
// 判断所有元素
// this.each(function() {
// hasCName = (" " + this.className + " ")
// .indexOf( " " + trim(cName) + " ") !== -1;
// if(hasCName) {
// hasCName = true;
// return false;
// }
// });
// return hasCName;
}
});
itcast.fn.extend({
addClass: function(cName) {
return this.each(function() {
var className = this.className;
className += " " + cName;
this.className = itcast.trim(className);
});
}
});
itcast.fn.extend({
removeClass: function(cName) {
return this.each(function() {
var cArr = this.className.split(" "),
i, len = cArr.length;
for(i = 0; i < len; i++) {
if(cArr[i] === cName) {
break;
}
}
cArr.splice(i, 1);
this.className = cArr.join(" ");
// 2 replace 替换
// var className = " " + this.className + " ";
// this.className = itcast.trim(className.replace(" " + cName + " ", " "));
// 处理多个相同类名的情况
// var clsName = " " + this.className.trim() + " ";
// while(clsName.indexOf(" " + name + " ") > -1) {
// clsName = clsName.replace(" " + name + " ", " ");
// }
// this.className = clsName.trim();
});
}
});
itcast.fn.extend({
toggleClass: function(cName) {
if(this.hasClass(cName)) {
this.removeClass(cName);
} else {
this.addClass(cName);
}
return this;
}
});
itcast.fn.extend({
attr: function(name, value) {
if(value === undefined) {
return this[0].getAttribute(name);
}
return this.each(function() {
// this.setAttribute(name, value);
this[name] = value;
});
}
});
itcast.fn.extend({
val: function(value) {
if(value === undefined) {
return this[0].value;
}
return this.each(function() {
this.value = value;
});
}
});
itcast.fn.extend({
html: function(html) {
if(html === undefined) {
return this[0].innerHTML;
}
return this.each(function() {
this.innerHTML = html;
});
}
});
itcast.extend({
getInnerText: function(dom) {
var textArr;
if(dom.innerText !== undefined) {
return dom.innerText;
}
textArr = getNodeText(dom);
return textArr.join("");
function getNodeText(node) {
var cNodes = node.childNodes,
len = cNodes.length, i, cNode,
arr = [];
for(i = 0; i < len; i++) {
cNode = cNodes[i];
if(cNode.nodeType === 3) {
arr.push(cNode.nodeValue);
} else if(cNodes.nodeType === 1) {
arr = arr.concat( getNodeText(cNode) );
}
}
return arr;
}
},
setInnerText: function(dom, str) {
if("innerText" in dom) {
dom.innerText = str;
} else {
dom.innerHTML = "";
dom.appendChild( document.createTextNode(str) );
}
}
});
itcast.fn.extend({
text: function(text) {
if(text === undefined) {
return itcast.getInnerText(this[0]);
}
return this.each(function() {
itcast.setInnerText(this, text);
});
}
});
标签:
原文地址:http://www.cnblogs.com/lsy0403/p/5912212.html