码迷,mamicode.com
首页 > 其他好文 > 详细

自己封装函数

时间:2018-01-23 01:08:57      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:ext   汉字   改变样式   tin   input   win   gpo   button   ret   

    <div id="cc" name="aa" class="zhao"></div>
  手机号:<input type="text" value="" id="sj"> <input type="button" value="确定"/>
    <script type="text/javascript">
      //1.封装id 
      function $(id){
          return  document.getElementById(id)
      }
      //2.封装 name         //var idd=$$("aa")[0].innerHTML
      function $$(name){
          return  document.getElementsByName(name);
      }
      //3.封装标签
      function $$$(tagName){
          return  document.getElementsByTagName(tagName)
      }
      var idd=$$$("div")[0].innerHTML;
      // console.log(idd)
      //4.封装时间
      function DangQianShiJian(){
    var nian= new Date().getFullYear();     // 获取当前年
    var yue= new Date().getMonth()+1;       // 获取当前月
    var ri= new Date().getDate();       // 获取当前日
    var shi= new Date().getHours();     // 获取当前时
    var fen= new Date().getMinutes();   // 获取当前分
    var miao= new Date().getSeconds();    // 获取当前秒
    var xq1=new Date();                
    var xq=["日","一","二","三","四","五","六"]     //把数字改成汉字
    if(yue<10){yue="0"+yue}
    if(miao<10){miao="0"+miao}
    if(shi<10){shi="0"+shi}
    if(fen<10){fen="0"+fen}
    if(ri<10){ri="0"+ri}
    return nian+"年"+yue+"月"+ri+"日"+shi+"时"+fen+"分"+miao+"秒"+"星期"+xq[xq1.getDay()]
  }
  // var bb=$("cc").innerHTML
  // console.log(bb=DangQianShiJian())
  //5.随机颜色
  function getColor(color){
    var r = parseInt(Math.random()*256)
    var g = parseInt(Math.random()*256)
    var b = parseInt(Math.random()*256)
    var rgb="rgb("+r+","+g+","+b+")"
    return rgb
  }
  // $("cc").style.color=getColor();  
  //6.封装改变样式
  function getStyle(label,Sty){      // label为元素, sty为样式
   if(label.currentStyle){
     return label.currentStyle[Sty];
   }else{
     return window.getComputedStyle(label,null)[Sty];
   }
  }
  //var cct=document.getElementById("lc");
  // console.log(window.getComputedStyle(cct,null)["height"])  //获取样式
  //console.log(parseInt(getStyle(cct,"width"))+8+"px");         //更改样式
  //7封装随机数
  function getSuiJiShu(n){
    var str =(‘1234567890abcdefghigklmnopqrstuvwsyzABCDEFGHIGKLMNOPQRSTUVWSYZ‘);
    var mto = ‘‘;
    for(var i=0;i<n;i++)
      mto += str.charAt(parseInt(Math.random()*str.length));    //charAt() 从某个字符串取得具体的字符。
       return mto
    
  }
   //console.log($("cc").innerHTML=getSuiJiShu(4))
//8.封装class
   function getClass(x){  
  return document.getElementsByClassName(x);
} 
//9.封装手机号
function Zhao_ShoujiHao(shouji){
  var yunhu2=$(shouji).value;
  var tiaojian=/^(1([3|8][0-9])|1([4][57])|1([5][012356789])|1([7][0678]))\d{8}$/
        if(tiaojian.test(yunhu2)){
      return console.log("完成注册")
      }else{
      return  console.log("请输入正确手机号")
      }
}
//10.封装
//封装lastIndexOf(找到字符串中倒数第n次出现的字符(正序或倒序))
//ar要查找的字符,y要找的倒数第几位,str为这个的字符串,c:true为倒序,false为正序.
  var str=("Stand out or Get out");
function j_lastIndexOf(char,y,str,c){

  var b=0;
  //从后向前
    if(c==true){
      for(var i=str.length-1;i>=0;i--){
        if(str[i]==char){
          b++;
          if(b==y){
            return i;//返回其下标
          }
        }
      }
    //从前向后
    }else if(c==false){
      for(var i=0;i<str.length;i++){
        if(str[i]==char){
          b++;
          if(b==y){
            return i;//返回其下标
            }
          }
        }
      }
    }
  document.write(j_lastIndexOf("o",2,str,true));

    </script>

 

自己封装函数

标签:ext   汉字   改变样式   tin   input   win   gpo   button   ret   

原文地址:https://www.cnblogs.com/zhaoxialong/p/8331580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!