zoukankan      html  css  js  c++  java
  • 常用的一些自定义函数

     1         function $id()
     2         {
     3             var obj,name;
     4             if( arguments.length==2 )
     5             {    
     6                 obj = arguments[0];
     7                 name = arguments[1];
     8             }else if( arguments.length==1 ){
     9                 obj = document;
    10                 name = arguments[0];
    11             }else{
    12                 return alert( "$id have error!" );
    13             }
    14             return obj.getElementById(name);
    15         }
    16         
    17         function $selectedOption(obj,selectedValue)
    18         {
    19             for( var i=0;i<obj.options.length;i++ )
    20             {
    21                 if( obj.options[i].value == selectedValue )
    22                 {
    23                     obj.options[i].setAttribute("selected","true");
                           obj.selectedIndex = i; //这行代码是后来加上去的,原因在IE中找不到selectedIndex
    24                     return;
    25                 }
    26             }
    27         }
    28         
    29         function $name()
    30         {
    31             var obj,name;
    32             if( arguments.length==2 )
    33             {    
    34                 obj = arguments[0];
    35                 name = arguments[1];
                       return obj[name]!==undefined ? obj[name]:obj.getElementsByName(name);
    36             }else if( arguments.length==1 ){
    37                 obj = document;
    38                 name = arguments[0];
                       return obj.getElementsByName(name);
    39             }else{
    40                 return alert( "$name have error!" );
    41             }
    43         }
    44         
    45         function $tag()
    46         {
    47             var obj,name;
    48             if( arguments.length==2 )
    49             {    
    50                 obj = arguments[0];
    51                 name = arguments[1];
    52             }else if( arguments.length==1 ){
    53                 obj = document;
    54                 name = arguments[0];
    55             }else{
    56                 return alert( "$tag have error!" );
    57             }            
    58             return obj.getElementsByTagName(name);
    59         }
    60     
    61         String.prototype.trim = function() {
    62             var str = this,
    63             str = str.replace(/^\\s\\s*/''),
    64             ws = /\\s/,
    65             i = str.length;
    66             while (ws.test(str.charAt(--i)));
    67             return str.slice(0, i + 1);
    68         }


  • 相关阅读:
    独立构件风格之C2风格
    Mysql远程授权报语法错误
    Vuforia3D模型上传
    Hololens Vuforia 物体识别
    C盘无损分区
    MRTK 当进入某个物体时调用的函数
    Java实现斐波那契数列的两种方法
    Leetcode452. 用最少数量的箭引爆气球(排序+贪心)
    Java对二维数组排序
    帮你理清js的继承
  • 原文地址:https://www.cnblogs.com/chy1000/p/1774256.html
Copyright © 2011-2022 走看看