jquery:attr()的方法

  • 设置属性
$("#id").attr("checked",true);
//attr() 方法也允许您同时设置多个属性。
$("#id").attr({
        "href" : "http://www.runoob.com/jquery",
        "title" : "jQuery 教程"
    });
//也提供回调函数。回调函数有两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。
$("#id").attr("href", function(i,origValue){
    return origValue + "/jquery"; 
  });
  • 删除属性
$('#id').removeAttr('checked');
  • attr和prop的区别
//对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。
//对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。
//property能够从attribute中得到同步。
//attribute不会同步property上的值。
//有true,false两个属性使用prop()(如'checked','selected','disabled'等)。

猜你喜欢

转载自www.cnblogs.com/xxzb/p/12654905.html