zoukankan      html  css  js  c++  java
  • radio赋值法

    一般都会使用attr来使选中:

    $("#DIV的ID input[name='radio的name'][value="'+动态传的radio的value值+'"]").attr('checked','true')来设置哪个值的radio被选中,但是这种方法试过了没有用,找到一种有效的方法,就是把attr改成prop

    $("#DIV的ID input[name='radio的name'][value="'+动态传的radio的value值+'"]").prop('checked','true')

    $("#addMedia input[name='adType'][value='"+$(this).attr("adsMedia-adType")+"']").prop('checked','true');

    jquery中attr与prop有什么区别呢?在网上查了下:

    .attr() : 获取匹配的元素集合中的第一个元素的属性的值 或 设置每一个匹配元素的一个或多个属性。 •.attr( attributeName ) •.attr( attributeName )

    •.attr( attributeName, value ) •.attr( attributeName, value )
    •.attr( attributes )
    •.attr( attributeName, function(index, attr) )
     
    .prop() : 获取匹配的元素集中第一个元素的属性(property)值或设置每一个匹配元素的一个或多个属性。 •.prop( propertyName ) •.prop( propertyName )
    •.prop( propertyName, value ) •.prop( propertyName, value )
    •.prop( properties )
    •.prop( propertyName, function(index, oldPropertyValue) )
     
    是参数有区别,attr()传入的是attributeName,而prop()传入的是propertyName,
     
    Attributes 和  Properties 区别:
    可以将attribute理解为“特性”,property理解为为“属性”从而来区分俩者的差异。
     
    如果把DOM元素看成是一个普通的Object对象,这个对象在其定义时就具有一些属性(property),比如把select的option当做一个对象:
    var option = {
    selected:false
    disabled:false
    attributes:[],
    ...
    }
    现在,我们一目了然了,attribute是一个特性节点,每个DOM元素都有一个对应的attributes属性来存放所有的attribute节点,它是一个类数组的容器。attributes的每个数字索引以名值对(name=”value”)的形式存放了一个attribute节点。而property就是一个属性,是一个以名值对(name=”value”)的形式存放在Object中的属性。
     
    那attributes是什么意思呢?英语不好,查一下
     
    jquery中attr和prop的区别介绍:
     •对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。
     •对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。
     
    举个栗子:
    <a href="http://www.baidu.com" target="_self" class="btn">百度</a>
    这个例子里<a>元素的DOM属性有“href、target和class",这些属性就是<a>元素本身就带有的属性,也是W3C标准里就包含有这几个属性,或者说在IDE里能够智能提示出的属性,这些就叫做固有属性。处理这些属性时,建议使用prop方法。
     
    <a href="#" id="link1" action="delete">删除</a>
    这个例子里<a>元素的DOM属性有“href、id和action”,很明显,前两个是固有属性,而后面一个“action”属性是我们自己自定义上去的,<a>元素本身是没有这个属性的。这种就是自定义的DOM属性。处理这些属性时,建议使用attr方法。
     
  • 相关阅读:
    LeetCode 1110. Delete Nodes And Return Forest
    LeetCode 473. Matchsticks to Square
    LeetCode 886. Possible Bipartition
    LeetCode 737. Sentence Similarity II
    LeetCode 734. Sentence Similarity
    LeetCode 491. Increasing Subsequences
    LeetCode 1020. Number of Enclaves
    LeetCode 531. Lonely Pixel I
    LeetCode 1091. Shortest Path in Binary Matrix
    LeetCode 590. N-ary Tree Postorder Traversal
  • 原文地址:https://www.cnblogs.com/ngy0217/p/9001304.html
Copyright © 2011-2022 走看看