zoukankan      html  css  js  c++  java
  • 封装自己的getClassName函数

    <!doctype html>
    <html lang="en">
     <head>
      <meta charset="UTF-8">
      <meta name="Generator" content="EditPlus®">
      <meta name="Author" content="">
      <meta name="Keywords" content="">
      <meta name="Description" content="">
      <title>getClessName</title>
     </head>
     <body>
      
    
      <input type="submit" id = "sub" class="ss confirm btn" value="提交"/>
    
      <script>
    
            
        window.onload = function(){
    
            var Opt = document.getElementById('sub');
    
            var getClass = function(className,tagName){
                
    
                if(document.getElementsByTagName){
                    
                    var Inp = document.getElementsByTagName(tagName);
                    for(var i=0; i<Inp.length; i++){
    
                        if((new RegExp('(\s|^)' +className +'(\s|$)')).test(Inp[i].className)){
                            
                            return Inp[i];
                        }
                    
                    }
                    
                }else if(document.getElementsByClassName){
                    return document.getElementsByClassName(className);
                }
            }
            
            //alert(getClass("confirm", "input"));
            var aa = getClass("confirm", "input");
            //alert(Inp);
            //alert(Inp);
    
            function getClass(className, targetName){
                var ele = [];
                var all = document.getElementsByTagName(targetName || "*");
                for(var i=0; i<all.length; i++){
                    if(all[i].className.match(new RegExp('(\s|^)'+confirm+'(\s|$)'))){
                        
                        ele[ele.length] = all[i];
                    
                    }
                }
                return ele;
            }
    
            function getObjsByClass(tagName, className){
            
                if(document.getElementsByClassName){
                    alert("document.getElementsByClassName");
                    return document.getElementsByClassName(className);
                }else{
                    var el = [];
                    var _el = document.getElementsByTagName(tagName);
                    for(var i=0; i<_el.length; i++){
                    
                        if(_el[i].className.indexOf(className) > -1){
                            alert(_el[i]);
                            el[_el.length] = _el[i];
                        }
                    }
                    alert(el);
                    return el;
                }
            }
    
    
            function addEvent(ev, type){
                var ev = ev || window.event;
                if(ev.addEventListener){
                    
                    ev.addEventListener(type, function(){
                        alert(ev.type);
                    }, false);
                }else if(ev.attachEvent){
                    
                    ev.attachEvent("on" + type, function(){
                    alert("ev.attachEvent");
                        
                    });
                }
            }
    
    
        /*var getClass = function (className, parentNode) {
    
             var node = null;
             var temps = [];
             if (parentNode != undefined) {
                 node = parentNode;
             } else {
                node = document;
             }
             if(node.getElementsByTagName){
                 var all = node.getElementsByTagName('*');
                 for (var i = 0; i < all.length; i ++) {
                  if ((new RegExp('(\s|^)' +className +'(\s|$)')).test(all[i].className)) {
                      temps.push(all[i]);
                   }
               }
                 return temps;
             }else{
                if(node.getElementsByClassName){
                
                    return document.getElementsByClassName(className);
                }
             }
         }*/
    
    
            //alert(getClass("confirm"));
            //var obj = getClass("confirm","input");
            //alert(obj);
            //alert(Opt);
            addEvent(aa, 'click');
    
            
        }
      </script>
     </body>
    </html>
  • 相关阅读:
    基于Netty实现高性能通信程序之传输协议编码与解码
    博客园停止文章更新,最新文章请访问 www.zhaoyafei.cn,多谢您的支持!
    再谈PHP错误与异常处理
    C语言之预处理
    【转】linux sort 命令详解
    GO语言之channel
    浅谈Yii-admin的权限控制
    【转】搞清FastCgi与PHP-fpm之间的关系
    网站添加第三方登陆(PHP版)
    【转】PHP的Trait 特性
  • 原文地址:https://www.cnblogs.com/double405/p/5592666.html
Copyright © 2011-2022 走看看