1 回答

TA貢獻(xiàn)5條經(jīng)驗(yàn) 獲得超2個(gè)贊
jQuery 1.6之前 ,.attr()方法在取某些 attribute 的值時(shí),會(huì)返回 property 的值,這就導(dǎo)致了結(jié)果的不一致。從 jQuery 1.6 開始, .prop()方法 方法返回 property 的值,而 .attr() 方法返回 attributes 的值。?
例如, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, 和 defaultSelected 應(yīng)使用.prop()方法進(jìn)行取值或賦值。 在jQuery1.6之前,這些屬性使用.attr()方法取得,但是這并不是元素的attr屬性。他們沒有相應(yīng)的屬性(attributes),只有特性(property)。?
例如,考慮一個(gè)DOM元素的HTML標(biāo)記中定義的<input type="checkbox" checked="checked" /> ,并假設(shè)它是一個(gè)JavaScript變量命名的elem :?
elem.checked true (Boolean) 將改變復(fù)選框的狀態(tài)?
$(elem).prop("checked") true (Boolean) 將改變復(fù)選框的狀態(tài)?
elem.getAttribute("checked") "checked" (String) 不會(huì)改變的復(fù)選框的初始狀態(tài);?
$(elem).attr("checked") (1.6) "checked" (String) 不會(huì)改變的復(fù)選框的初始狀態(tài);?
$(elem).attr("checked") (1.6.1+) "checked" (String) 將改變復(fù)選框的狀態(tài)?
$(elem).attr("checked") (pre-1.6) true (Boolean) 將改變復(fù)選框的狀態(tài)?
根據(jù)W3C的表單規(guī)范 ,在checked屬性是一個(gè)布爾屬性,這意味著只要該 attribute 存在,即使它沒有值,或是一個(gè)空字符串,該屬性對(duì)應(yīng)的 property 就是 true。
更改后的代碼如下:
<script type="text/javascript">
? ? ? ? $(function () {
? ? ? ? ? ? $("#allorno").click(function () {
? ? ? ? ? ? ? ? $("input[name='items']").prop("checked", $(this).prop("checked"));
? ? ? ? ? ? });
? ? ? ? });
? ? </script>
- 1 回答
- 0 關(guān)注
- 1178 瀏覽
添加回答
舉報(bào)