JavaScript_JavaScript 扩展函数


JavaScript 是一门强大的语言,基础的函数比较丰富,但仍不能满足我们的需求。


下面列出一些常用的扩展函数   


持续更新中。。。 有更好的扩展函数 的小伙伴欢迎分享


1.window.onload 加载多个函数

function addLoadEvent(func)
http://blog.csdn.net/u010003835/article/details/51346045




2.在指定节点之后插入新节点

function insertAfter(newElement, targetElement)

http://blog.csdn.net/u010003835/article/details/51346209




3.查找指定节点之后的下一个元素节点   

function getNextElement(node)  

http://blog.csdn.net/u010003835/article/details/51346264



4.给一个DOM结点 增加一个class类 的方法 

function addClass(element, value)
http://blog.csdn.net/u010003835/article/details/51347941


5.查看input元素是否支持某种type

function inputSupportsType(type)
http://blog.csdn.net/u010003835/article/details/51356192


6.检查某元素是否有某属性 

function elementSupportAttribute(elementName,attribute)
http://blog.csdn.net/u010003835/article/details/51356250




源代码

 /**
 * Edit by szh on 2016/5/9.
 */

//扩展window.onload 使其能加载多个JavaScript函数
//功能:在window.onload 添加 新函数func
function addLoadEvent(func){
    if(typeof func != 'function') return false;
    var oldonload = window.onload;
    if(typeof window.onload != 'function'){
        window.onload = func;
    }else{
        window.onload = function(){
            oldonload();
            func();
        }
    }
}


//功能: 在targetElement之后插入 新节点newElement
function insertAfter(newElement, targetElement){
    var parent = targetElement.parentNode;
    if(parent.lastChild == targetElement){
        parent.appendChild(newElement);
    }else{
        parent.insertBefore(newElement,targetElement.nextSibling);
    }
}

//功能: 查找node节点的下一个元素节点 node为当前节点
// ** node.nexSibling 得到节点的下一个节点, 不一定是元素节点
function getNextElement(node){
    if(!node.nextSibling) return null;
    var nextNode = node.nextSibling;
    if(nextNode.nodeType == 1){
        return nextNode;
    }
    return getNextElement(node.nextSibling);
}

//功能:给节点加一个CSS类 value,value为字符串格式
//示例 addClass(document.getElementById('test'),'mouseoverHighLight');
function addClass(element, value){
    if(!element.className){
        element.className = value;
    }else{
        newClassName = element.className;
        newClassName += " ";
        newClassName += value;
        element.className = newClassName;
    }
}

 //功能: 对HTML5的 input type 属性进行检查,看是否支持该属性
 //示例
 //console.log("input type是否支持url类型"+inputSupportsType('url'));
 //console.log("input type是否支持kk类型"+inputSupportsType('kk'));
 function inputSupportsType(type){
     if(!document.createElement) return false;
     var input = document.createElement('input');
     input.setAttribute('type',type);
     if(input.type=='text' && type!='text'){
         return false;
     }
     else{
         return true;
     }
 }

 //功能: 看elementName所对应的元素里 是否支持attribute属性
 //示例
 //console.log("input是否支持placeholder属性"+elementSupportAttribute('input','placeholder'));
 //console.log("input是否支持placeholde属性"+elementSupportAttribute('input','placeholde'));
 function elementSupportAttribute(elementName,attribute){
     if(!document.createElement) return false;
     var elem = document.createElement(elementName);
     return (attribute in elem);
 }






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值