jQuery 用 prop() 取代部分 attr()
在jquery 1.6以前,可以用attr取得某元素屬性的值,比如$('#img').attr('alt');
可是如果遇到像要取得checkbox的disabled屬性,有的瀏覽器要用disabled="disabled",有些則用disabled就可以了,所以在1.6版以後,就用prop,不過不一樣的是回傳的是true/false,適用此點的Boolean屬性包含了: autofocus, autoplay, async, checked, controls, defer, disabled, hidden, loop, multiple, open, readonly, required, scoped, selected
jQuery也有提供DOM元素屬性適用attr()/prop()的對照表
參考
jQuery 1.6.1上場救援,不用改寫attr()囉
可是如果遇到像要取得checkbox的disabled屬性,有的瀏覽器要用disabled="disabled",有些則用disabled就可以了,所以在1.6版以後,就用prop,不過不一樣的是回傳的是true/false,適用此點的Boolean屬性包含了: autofocus, autoplay, async, checked, controls, defer, disabled, hidden, loop, multiple, open, readonly, required, scoped, selected
jQuery也有提供DOM元素屬性適用attr()/prop()的對照表
Attribute/Property | .attr() | .prop() |
---|---|---|
accesskey | ✓ | |
align | ✓ | |
async | ✓ | ✓ |
autofocus | ✓ | ✓ |
checked | ✓ | ✓ |
class | ✓ | |
contenteditable | ✓ | |
defaultValue | ✓ | |
draggable | ✓ | |
href | ✓ | |
id | ✓ | |
label | ✓ | |
location * | ✓ | ✓ |
multiple | ✓ | ✓ |
nodeName | ✓ | |
nodeType | ✓ | |
readOnly | ✓ | ✓ |
rel | ✓ | |
selected | ✓ | ✓ |
selectedIndex | ✓ | |
src | ✓ | |
style | ✓ | |
tabindex | ✓ | |
tagName | ✓ | |
title | ✓ | |
type | ✓ | |
width ** | ✓ |
參考
jQuery 1.6.1上場救援,不用改寫attr()囉
留言