前端研习社

封装JS方法_增删改查

2017-06-16  本文已影响56人  林立镇
window.dom = {};
window.dom.attr = function(tag,attr){
    for(var key in attr){
        tag.setAttribute(key,attr[key]);
    }
};
window.dom.style = function(tag,attr){
    for(var key in attr){
        tag.style[key] = attr[key];
    }
};
window.dom.find = function(selector,scope){
    var tag = (scope instanceof HTMLElement) ? scope : document;
    return tag.querySelectorAll(selector);
};
window.dom.children = function(tag){
    return tag.children;
};
window.dom.text = function(tag){
    var text = '';
    for(var i = 0;i < tag.childNodes.length;i++){
        if(tag.childNodes[i].nodeType === 3){
             text += tag.childNodes[i].textContent.trim();
        }
    }
    return text;
};
window.dom.bigBrother = function(tag){
    var previous = tag.previousSibling || null;
    while(previous !== null && previous.nodeType !== 1){
        previous = previous.previousSibling;
    }
    return previous;
}
上一篇下一篇

猜你喜欢

热点阅读