zoukankan      html  css  js  c++  java
  • attr与prop

    Jquery获取checkbox属性checked为undefined (2014-06-12 19:51:22)转载▼
    标签: js jquery checkbox checked undefined    分类: JQuery
    使用jQuery v1.10.2获取checkbox的状态时,用.attr("checked")时输出总是为undefined.郁闷了,这难道是个bug?!
    
    查看jQuery API的文档,发现:
    As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. In addition, .attr() should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the .prop() method.
    The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.
    
    也就说:v1.6以后attr(‘checked’)就返回checked和undefined,v1.6以前返回true和false,v1.6以后可以使用is(‘:checked’)或者.prop(‘checked’)来返回true和false
    
    总结:
    (1)获取checked的方法
        .attr('checked'):  
        .prop('checked'): //1.6+:true/false
        .is(':checked'):   
    (2)checked赋值
    
         所有的jquery版本都可以这样赋值:
         .attr("checked","checked");
         .attr("checked",true);
    
          jquery1.6以上版本的:
         .prop("checked",true);
        .prop("checked","checked");
        .prop({checked:true});
         .prop("checked",function(){
                  return true;
          });
    
    (3)note:jquery1.6以上才存在prop();
  • 相关阅读:
    C++ Sqlite3的基本使用
    DirectX11 初探XMVECOTR&XMMATRIX
    lib和dll文件的初了解
    游戏设计模式——C++单例类
    C++11智能指针的深度理解
    你的文章里为什么不放源码Github链接了
    堡垒机的核心武器:WebSSH录像实现
    Asciinema文章勘误及Web端使用介绍
    Asciinema:你的所有操作都将被录制
    Django实现WebSSH操作物理机或虚拟机
  • 原文地址:https://www.cnblogs.com/itboy-2009/p/4772846.html
Copyright © 2011-2022 走看看