屬性只有一個值的這類 html 屬性是怎麼回事,該如何設定值;比如:checked = “checked” vs checked = true,checkedvs

來源:互聯網
上載者:User

屬性只有一個值的這類 html 屬性是怎麼回事,該如何設定值;比如:checked = “checked” vs checked = true,checkedvs

參考連結:https://stackoverflow.com/questions/10650233/checked-checked-vs-checked-true

問:

1 What is the difference between the below two usages?2 3 document.getElementById('myRadio').checked = "checked";4 and5 6 document.getElementById('myRadio').checked = true;7 For me, both are behaving the same way. But, I am just curious to know why there exist two methods to do the same.8 9 Which one will be the ideal usage? I need to support IE7 and higher versions.View Code

答:

1 document.getElementById('myRadio').checked is a boolean value. It should be true or false2 3 document.getElementById('myRadio').checked = "checked"; casts the string to a boolean, which is true.4 5 document.getElementById('myRadio').checked = true; just assigns true without casting.6 7 Use true as it is marginally more efficient and is more intention revealing to maintainers.View Code

document.getElementById('myRadio')。checked是一個布爾值。 這應該是真的或假的

document.getElementById('myRadio')。checked =“checked”; 將字串轉換為布爾值,這是正確的。

document.getElementById('myRadio')。checked = true; 只是分配true而不投射。

使用true,因為它的效率稍微高一點,而且更有意向維護者展示。

所以設定這種單屬性的值為 true 或 false 就對了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.