www.bifa88.com 1

JavaScript兑现复选框的全选

方法一:

  • 创建集合

替换方法的作用一般是在原方法的基础上加上我们想要加的功能,就是改变其IMP,所以我们调用的时候还是调用原方法,不需要去修改

<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script type="text/javascript" src="01.js"></script>
</head>
<body>
<input type="button" id="All" value="全选" /><br />
<input type="button" id="uncheck" value="不选" /><br />
<input type="button" id="othercheck" value="反选" /><br />
<div id="div">
<input type="checkbox" /><br />
<input type="checkbox" /><br />
<input type="checkbox" /><br />
</div>
<script type="text/javascript" src="Untitled-12.js"></script>
</body>
</html>

     var CheckAll=document.getElementById('All');
     var UnCheck=document.getElementById('uncheck');
     var OtherCheck=document.getElementById('othercheck');
     var div=document.getElementById('div');
     var CheckBox=div.getElementsByTagName('input');
     CheckAll.onclick=function()
     {
            for(i=0;i<CheckBox.length;i++){
                    CheckBox[i].checked=true;
                }
     }
     UnCheck.onclick=function()
     {
            for(i=0;i<CheckBox.length;i++){
                   CheckBox[i].checked=false;
                 }
     }
     othercheck.onclick=function()
     {
            for(i=0;i<CheckBox.length;i++)
     {
                    if(CheckBox[i].checked==true)  /*注意这里是双等于号;一个等于号是赋值的意思,双等于号是判断的意思*/
                        {
                            CheckBox[i].checked=false;
                        }
                     else{
                        CheckBox[i].checked=true                       
                     }

     }
}

一. 在分类中重写之前的方法(即覆盖了原方法)

在分类中重写之前的方法:
People.h

@interface People : NSObject
- (void)say ;
@end

People +Swizzling.m

#import "People +Swizzling.h"

@implementation People (Swizzling)
- (void)say {
    NSLog(@"People +Swizzling.h  --- say");
}
@end

但是这样做的缺点有2点:
1.在分类中重写方法会覆盖掉原方法的功能(即调用不了原方法),
2,如果多个分类都重写了一个方法,运行时机制无法确定会一直调用哪个方法;

方法二:

    /**
     *  @param mixed $value
     *  @return \Illuminate\Support\Collection
     *  辅助函数 collect 创建 Collection 对象
     */
     Collection function collect($value = null);
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    /**
     *  @param mixed $value
     *  @return \Illuminate\Support\Collection
     *  采用 new 关键字创建Collection集合对象
     */
    new \Illuminate\Support\Collection($value);

二. 使用运行时的交换方法(method swizzling)

写一个不同名的方法, 使用运行时将该方法与原方法交换,
(子类的方法列表并不包含父类中的方法)

  • 分2种情况:
  • 原方法为父类方法,子类没有
  • 方法和替换方法都实现于子类

Method m1 = class_getInstanceMethod(self, @selector(eat));
   Method m2 = class_getInstanceMethod(self, @selector(son_eat));

   BOOL add = class_addMethod(self, @selector(eat), method_getImplementation(m2), method_getTypeEncoding(m2));

1.第一种情况:子类没有实现替换方法时,需要检测(将原方法名SEL和替换方法的实现IMP,加入方法到本类中),
如果能加入,这时原方法的方法实现指针指向了替换方法,
再将替换方法SEL的IMP指向原方法的IMP就完成了方法交换;

if (add) {
        class_replaceMethod(self, @selector(son_eat), method_getImplementation(m1), method_getTypeEncoding(m1));
    }

2.第二种情况:俩个方法都在子类中已经实现,就直接交换方法即可

else {
       method_exchangeImplementations(m1, m2);

   }

- (void)eat {
    NSLog(@"%s", __FUNCTION__);
}

- (void)son_eat {

    [self son_eat];

    NSLog(@"%s", __FUNCTION__);
}

确保该代码只会调用一次,避免多次调用替换混乱
一般是在+ (void)load 方法中

www.bifa88.com 1

www.bifa88.com,方法交换

<html>
    <head><meta charset="UTF-8">
     <script type="text/javascript" defer="defer" src="01.js"></script>
    </head>
    <body>
        <div id="div">
            <input type="checkbox" /><br />
            <input type="checkbox" /><br />
            <input type="checkbox" /><br />
            <input type="checkbox" /><br />
            <input type="checkbox" /><br />
            <input type="checkbox" /><br />
            <input type="checkbox" /><br />
            <input type="checkbox" /><br />
        </div>
        <input type="button" value="全选" onclick="CheckAll()"/><br />
        <input type="button" value="不选" onclick="UnCheck()"/><br />
        <input type="button" value="反选" onclick="othercheck()"/><br />
    </body>
</html>

            var CheckBox=div.getElementsByTagName('input');

            //全选
            function CheckAll(){
                for(i=0;i<CheckBox.length;i++){CheckBox[i].checked=true;};
            };

            //不选
            function UnCheck(){
                for(i=0;i<CheckBox.length;i++){CheckBox[i].checked=false;};
                };

            //反选
            function othercheck(){
                for(i=0;i<CheckBox.length;i++){
                    if(CheckBox[i].checked==true)    /*注意这里是双等于号;一个等于号是赋值的意思,双等于号是判断的意思*/

                    { 
                     CheckBox[i].checked=false;
                    }
                    else{ CheckBox[i].checked=true}
                }
            };
  • all 方法

三.C指针

  • (方法替换的本质:将原方法的方法实现指向一个新的方法,新方法里面需要调用原方法的实现,即新方法是私有的;)

void (*gOrigDrawRect)(id, SEL, NSRect);
+ (void)load
{
        Method origMethod = class_getInstanceMethod(self, @selector(drawRect:));
        gOrigDrawRect = (void *)method_getImplementation(origMethod);
 if(!class_addMethod(self, @selector(drawRect:), (IMP)OverrideDrawRect, method_getTypeEncoding(origMethod)))
            method_setImplementation(origMethod, (IMP)OverrideDrawRect);
}

static void OverrideDrawRect(NSView *self, SEL _cmd, NSRect r)
{
        gOrigDrawRect(self, _cmd, r);
        [[NSColor blueColor] set];
        NSRectFill(r);
}

参考
  • Method Replacement for Fun and
    Profit
    /**
     * @return array
     * 得到当前Collection的值 $this->items
     */
     public function all();
  • map 方法

# 参看 array_map() 函数
$collection = collect(array('hello'=>"miss",'php'=>"mr",'or','golang'));
# 遍历集合数组元素(返回插入集合数值)
$coll = $collection->map(function ($value,$key){
    # $value 值 $key 键
    return "a12";
});
dump($coll); //> collect(array('hello'=>"a12",'php'=>"a12","a12","a12"))
  • reject 方法

# 过滤集合中元素参看 array_filter() 方法
$collection = collect(array('hello'=>"miss",'php'=>"mr",'or','golang'));
# 遍历集合数组元素(返回插入集合数值)
$coll = $collection->reject(function ($value,$key){
      # $value 值 $key 键
      # 返回值 bool:true 过滤该值
    if( $key === 'php' ){
        return true;
    }
    return false;
});
dump($coll); //> collect(array('hello'=>"miss,'or','golang'))
  • avg 方法

$collection = collect(array('hello'=>"miss",'php'=>2,'or',3))->avg(); //> 不推荐
dump($collection); //> 1.25 = (2+3)/4
$collection = collect(array(1,3,5,7,9))->avg();
dump($collection); //> 5 = (1+3+5+7+9)/5
$collection = collect(array(array('php'=>12,13,43),array('es'=>12,13,'php'=>32)))->avg('php');
dump($collection); //> 22 = (12+32)/2
  • max 方法

# max 推荐纯数值集合
$collection = collect(array('hello'=>"miss",'php'=>2,'or',3))->max();  //> 不推荐
dump($collection); //> 3
$collection = collect(array(1,3,5,7,9))->max();
dump($collection); //> 9
$collection = collect(array(array('php'=>12,13,43),array('es'=>12,13,'php'=>32)))->max('php');
dump($collection); //> 32
  • min 方法

# min 推荐纯数值集合
$collection = collect(array('hello'=>"miss",'php'=>2,'or',3))->min(); //> 不推荐
dump($collection); //> miss
$collection = collect(array(1,3,5,7,9))->min();
dump($collection); //> 1
$collection = collect(array(array('php'=>12,13,43),array('es'=>12,13,'php'=>32)))->min('php');
dump($collection); //> 12
  • sum 方法

$collection = collect(array('hello'=>"miss",'php'=>2,'or',3))->sum();
dump($collection); //> 5
$collection = collect(array(1,3,5,7,9))->sum();
dump($collection); //> 25
$collection = collect(array(array('php'=>12,13,43),array('es'=>12,13,'php'=>32)))->sum('php');
dump($collection); //> 44
  • tap 方法

    /**
     * Pass the collection to the given callback and then return it.
     *
     * @param  callable  $callback
     * @return $this
     */
    public function tap(callable $callback)
    {
        $callback(new static($this->items));

        return $this;
    }
    $collection = collect(array('hello'=>"miss",'php'=>2,'or',3));
    $collection->tap(function($coll){
        dump($coll); //> collect(array('hello'=>"miss",'php'=>2,'or',3))
    });
  • toArray 方法

    /**
     * Get the collection of items as a plain array.
     *
     * @return array
     */
    public function toArray()
    {
        return array_map(function ($value) {
            return $value instanceof Arrayable ? $value->toArray() : $value;
        }, $this->items);
    }
  • toJson 方法

    /**
     * Get the collection of items as JSON.
     *
     * @param  int  $options
     * @return string
     */
    public function toJson($options = 0)
    {
        return json_encode($this->jsonSerialize(), $options);
    }
    /**
     * Convert the object into something JSON serializable.
     *
     * @return array
     */
    public function jsonSerialize()
    {
        return array_map(function ($value) {
            if ($value instanceof JsonSerializable) {
                return $value->jsonSerialize();
            } elseif ($value instanceof Jsonable) {
                return json_decode($value->toJson(), true);
            } elseif ($value instanceof Arrayable) {
                return $value->toArray();
            } else {
                return $value;
            }
        }, $this->items);
    }
  • zip 方法

    /**
     * Zip the collection together with one or more arrays.
     *
     * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]);
     *      => [[1, 4], [2, 5], [3, 6]]
     *
     * @param  mixed ...$items
     * @return static
     */
    public function zip($items)
    {
        $arrayableItems = array_map(function ($items) {
            return $this->getArrayableItems($items);
        }, func_get_args());

        $params = array_merge([function () {
            return new static(func_get_args());
        }, $this->items], $arrayableItems);

        return new static(call_user_func_array('array_map', $params));
    }
  • median 方法

# 获取给定集合的中间值(排序后的中间值)
# 集合是奇数 获取中间数值
# 集合是偶数 获取中间两个数的平均值
$collection = collect(array('hello'=>34,'php'=>2,'we'=>32,'ed'=>54,'wd'=>3,43));
dump($collection->median());  //> 33 = (34+32)/2
$collection = collect(array('hello'=>34,'php'=>2,'we'=>32,'ed'=>54,'wd'=>3));
dump($collection->median()); //> 32
$collection = collect(array(array(12,11,'php'=>43),array(12,1,'php'=>23),array('php'=>12,3)));
dump($collection->median('php')); //> 23
  • mode 方法

# 返回出现次数最多的数
$collection = collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13),array('php'=>12,3)));
dump($collection->mode('php')); //> [13]
$collection = collect(array('hello'=>34,'php'=>2,'we'=>34,'ed'=>34,'wd'=>3,2,2));
dump($collection->mode());  //> [34,2]
$collection = collect(array(12,3,234,21,32,123,1,1,2,2));
dump($collection->mode());  //> [1,2]
  • each 方法

    /**
     * Execute a callback over each item.
     *
     * @param  callable  $callback
     * @return $this
     */
    public function each(callable $callback)
    {
        foreach ($this->items as $key => $item) {
            if ($callback($item, $key) === false) {
                break;
            }
        }

        return $this;
    }
$collection = collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13),array('php'=>12,3)));
$collection->each(function ($value,$key){
    # $value 值 $key 键
    dump($value);
    dump($key);
});
  • every 方法

# 检查集合中每一个元素是否符合条件
# 源码参考
    /**
     * Determine if all items in the collection pass the given test.
     *
     * @param  string|callable  $key
     * @param  mixed  $operator
     * @param  mixed  $value
     * @return bool
     */
    public function every($key, $operator = null, $value = null)
    {
        if (func_num_args() == 1) {
            $callback = $this->valueRetriever($key);

            foreach ($this->items as $k => $v) {
                if (! $callback($v, $k)) {
                    return false;
                }
            }

            return true;
        }

        if (func_num_args() == 2) {
            $value = $operator;

            $operator = '=';
        }

        return $this->every($this->operatorForWhere($key, $operator, $value));
    }
# 使用示例
$collection = collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13),array('php'=>12,3)));
$result = $collection->every(function($v,$k){
    # $v 值 $k 键
    # 条件判断 
    # return false; 比较失败终止比较返回false
    # return true; 本次比较通过,进行下次比较
    return true;
});
dump($result);    //> true
dump($collection->every('php','>',12)); //> false
# php键 是否存在集合里(集合里的每一个数组中)
dump($collection->every('php')); //> true 
dump(collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13),array(3)))->every('php')); //> false
# 支持比较符 null(=) = == != <> > < >= <= === !==
# 更多用法 - 参看源码
  • when 方法

# 源码 - when 方法使用很简单这里不详细举例了
    /**
     * Apply the callback if the value is truthy.
     *
     * @param  bool  $value            # 判断条件
     * @param  callable  $callback
     * @param  callable  $default
     * @return mixed
     */
    public function when($value, callable $callback, callable $default = null)
    {
        if ($value) {
            return $callback($this);
        } elseif ($default) {
            return $default($this);
        }

        return $this;
    }
  • unless 方法

# 源码 - 参考when方法
    /**
     * Apply the callback if the value is falsy.
     *
     * @param  bool  $value            # 判断条件
     * @param  callable  $callback
     * @param  callable  $default
     * @return mixed
     */
    public function unless($value, callable $callback, callable $default = null)
    {
        return $this->when(! $value, $callback, $default);
    }
  • where 方法

# 过滤掉其他数据 刷选
    /**
     * Filter items by the given key value pair.
     *
     * @param  string  $key
     * @param  mixed  $operator
     * @param  mixed  $value
     * @return static
     */
    public function where($key, $operator, $value = null)
    {
        if (func_num_args() == 2) {
            $value = $operator;

            $operator = '=';
        }

        return $this->filter($this->operatorForWhere($key, $operator, $value));
    }
# 使用示例
$collection = collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13),array(3)));
dump($collection->where('php','=',13)); //> collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13)))
# 支持比较符 null(=) = == != <> > < >= <= === !==
  • whereStrict 方法

# 参考where方法 严格等于 筛选
    /**
     * Filter items by the given key value pair using strict comparison.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return static
     */
    public function whereStrict($key, $value)
    {
        return $this->where($key, '===', $value);
    }
  • whereIn 方法

# 源码 - 给定集合键(过滤集合)是否在另一个数组或集合里存在
    /**
     * Filter items by the given key value pair.
     *
     * @param  string  $key       # 给定集合里的键
     * @param  mixed  $values # 另一个集合或数组
     * @param  bool  $strict       # 是否开启严格比较
     * @return static
     */
    public function whereIn($key, $values, $strict = false)
    {
        $values = $this->getArrayableItems($values);

        return $this->filter(function ($item) use ($key, $values, $strict) {
            return in_array(data_get($item, $key), $values, $strict);
        });
    }
  • whereInStrict 方法

# 严格过滤 - 参看whereIn方法
    /**
     * Filter items by the given key value pair using strict comparison.
     *
     * @param  string  $key
     * @param  mixed  $values
     * @return static
     */
    public function whereInStrict($key, $values)
    {
        return $this->whereIn($key, $values, true);
    }
  • whereNotIn 方法

# 该方法正好和whereIn用法相反
    /**
     * Filter items by the given key value pair.
     *
     * @param  string  $key
     * @param  mixed  $values
     * @param  bool  $strict
     * @return static
     */
    public function whereNotIn($key, $values, $strict = false)
    {
        $values = $this->getArrayableItems($values);

        return $this->reject(function ($item) use ($key, $values, $strict) {
            return in_array(data_get($item, $key), $values, $strict);
        });
    }
  • whereNotInStrict 方法

# 该方法和whereInStrict用法相反
    /**
     * Filter items by the given key value pair using strict comparison.
     *
     * @param  string  $key
     * @param  mixed  $values
     * @return static
     */
    public function whereNotInStrict($key, $values)
    {
        return $this->whereNotIn($key, $values, true);
    }