• 封装自己的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>
  • 相关阅读:
    移动 WEB 开发布局方式 ---- flex 布局
    使用 flex布局 制作携程网首页
    移动 WEB 开发布局方式 ---- rem 适配布局
    使用 flexible.js + rem 制作苏宁移动端首页
    移动 WEB 布局方式之 rem 适配布局 ---- 苏宁首页案例制作
    移动 WEB 开发的布局方式 ---- 响应式布局
    简述 JavaScript 的执行机制
    vuex 的使用详解
    .Net Core — 依赖注入
    .NET Core +Angular 项目 部署到CentOS
  • 原文地址:https://www.cnblogs.com/double405/p/5592666.html
Copyright © 2020-2023  润新知