zoukankan      html  css  js  c++  java
  • prop()和attr()

    attributesproperties(属性值)之间的差异在特定情况下是很重要。jQuery 1.6之前 ,.attr()方法在取某些 attribute 的值时,会返回 property 的值,这就导致了结果的不一致。从 jQuery 1.6 开始, .prop()方法 方法返回 property 的值,而 .attr() 方法返回 attributes 的值。

    例如, selectedIndextagNamenodeNamenodeTypeownerDocumentdefaultChecked, 和 defaultSelected应使用.prop()方法进行取值或赋值。 在jQuery1.6之前,这些属性使用.attr()方法取得,但是这并不是元素的attr属性。他们没有相应的属性(attributes),只有特性(property)。

    例如,考虑一个DOM元素的HTML标记中定义的<input type="checkbox" checked="checked" /> ,并假设它是一个JavaScript变量命名的elem :

    elem.checked true (Boolean) 将随着复选框状态的改变而改变
    $(elem).prop("checked") true (Boolean) 将随着复选框状态的改变而改变
    elem.getAttribute("checked") "checked" (String) 复选框的初始状态;不会改变
    $(elem).attr("checked") (1.6) "checked" (String) 复选框的初始状态;不会改变
    $(elem).attr("checked") (1.6.1+) "checked" (String) 将随着复选框状态的改变而改变
    $(elem).attr("checked") (pre-1.6) true (Boolean) 将随着复选框状态的改变而改变

    你对生活笑,生活不会对你哭。
  • 相关阅读:
    理解 es7 async/await
    es6 Promise对象
    es6 Object.assign(target, ...sources)
    关于索引的一些知识
    关于this和$(this)
    js自定data-*和jquery的data()用法
    unslider插件实现轮播图效果
    清除浮动的方法
    事件绑定、事件监听和事件委托
    node.js的安装及配置
  • 原文地址:https://www.cnblogs.com/adanxiaobo/p/7633129.html
Copyright © 2011-2022 走看看