1、prop()方法应该用于解锁属性值,例如DOM属性(如selectedIndex,tagName,nodeName,nodeType,ownerDocument, defaultChecked, 和 defaultSelected)。

提示:如需检索 HTML 属性,请使用 attr() 方法代替。

提示:如需移除属性,请使用 removeProp()方法。

2、返回属性的值:

  $(selector).prop(property);

3、设置属性和值:

  $(selector).prop(property,value);

4、使用函数设置属性和值:

  $(selector).prop(property,function(index,currentvalue));

5、设置多个属性和值:

  $(selector).prop({property:valueproperty:value,...})

例子:<p><b>注意:</b>确认或取消选中该复选框,然后单击按钮刷新内容。</p>
<button>查看attr() 和 prop() 的值</button>
<br><br>
<input ></p>

$(document).ready(function(){
$("button").click(function(){
$("#p1").html("attr('checked'): " + $("input").attr('checked')
+ "<br>prop('checked'): " + $("input").prop('checked'));
});
});

注意:确认或取消选中该复选框,然后单击按钮刷新内容。

 

Check me

结果——attr('checked'): checked
prop('checked'): true

 

 
 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-04-26
  • 2022-12-23
  • 2021-08-03
  • 2022-12-23
  • 2021-04-08
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-01
  • 2021-11-24
  • 2022-12-23
相关资源
相似解决方案