1.5.2から、1.6.1へのアップグレード
新しい.prop()メソッドの導入と、.attr()メソッドへの変更によって、jQuery1.6は、属性とプロパティの違いや関連性について喧々囂々の議論を繰り広げることになり、1.6.1で既に修正にされた後方互換性の問題も、それに加わったが、1.5.2から1.6.1へのアップグレードにあたっては、何も属性コードに変更を加える必要はない。
With the introduction of the new .prop() method and the changes to the .attr() method, jQuery 1.6 sparked a discussion about the difference between attributes and properties and how they relate to each other. It also came with some backwards compatibility issues that have been fixed in 1.6.1. When updating from 1.5.2 to 1.6.1, you should not have to change any attribute code.
以下は、.attr()メソッドと.prop()メソッド好ましい使い方とjQuery 1.6と1.6.1のAttributeモジュールの変更点についての説明である。しかしながら、前述のとおり、jQuery 1.6,1は、あらゆる状況で、前に使われていたのと全く同じように.attr()属性を使うことを可能にしている。
Below is a description of the changes to the Attributes module in jQuery 1.6 and 1.6.1, as well as the preferred usage of the .attr() method and the .prop() method. However, as previously stated, jQuery 1.6.1 will allow you to use .attr() just as it was used before in all situations.
.data()メソッドに関しては、1.6のリリースノートに書いたような変更は修正され、1.5.2と1.6.1間ではシームレスに動作するようになった。
Note that the changes described in the 1.6 release notes regarding the .data() method have been worked around and now work seamlessly between 1.5.2 and 1.6.1.
何が変わったのか
Attributeモジュールへの変更によって、属性とプロパティのまぎらわしさは消えたが、1.6以前の全てのバージョンで、属性とプロパ ティをattr()という一つのメソッドで扱っていたため、 jQueryコミュニティに混乱を招いた。古い .attr()メソッドは沢山のバグがあり、維持することが困難だったのだ。
The changes to the Attributes module removed the ambiguity between attributes and properties, but caused some confusion in the jQuery community, since all versions of jQuery prior to 1.6 have handled attributes and properties in one method(.attr()). The old .attr() method had many bugs and was hard to maintain.
jQuery 1.6.1では、Attributeモジュールのアップデートとともに、いくつかのバグ修正が行われた。
jQuery 1.6.1 comes with several bug fixes as well as an update to the Attributes module.
特に、1.6.1における、checked, selected, readonly, そして, disabledなどといった真偽(Boolean)属性は、1.6以前と同様に扱われる。これによって、
$(“:checkbox”).attr(“checked”, true);
$(“option”).attr(“selected”, true);
$(“input”).attr(“readonly”, true);
$(“input”).attr(“disabled”, true);
や
if ( $(“:checkbox”).attr(“checked”) ) { /* Do something */ }
という、コードでさえ、1.6.1では変更は必要ない。
Specifically, boolean attributes such as checked, selected, readonly, and disabled in 1.6.1 will be treated just as they used to be treated in jQuery versions prior to 1.6. This means that code such as
$(“:checkbox”).attr(“checked”, true);
$(“option”).attr(“selected”, true);
$(“input”).attr(“readonly”, true);
$(“input”).attr(“disabled”, true);
or even:
if ( $(“:checkbox”).attr(“checked”) ) { /* Do something */ }
will not need to be changed in 1.6.1 in order to work as previously expected.
1.6.1における.attr()への変更点をはっきりさせると、以下のようになる。これらが、今までのバージョンで.attr()属性として機能していたもので、.prop()で置き換えられるべきケースである。
To make the changes to .attr() in jQuery 1.6 clear, here are some examples of the use cases of .attr() that worked in previous versions of jQuery that should be switched to use .prop() instead:
| .attr() |
適切な .prop() の用法 |
| $(window).attr… |
$(window).prop… |
| $(document).attr… |
$(document).prop… |
| $(“:checkbox”).attr(“checked”, true); |
$(“:checkbox”).prop(“checked”, true); |
| $(“option”).attr(“selected”, true); |
$(“option”).prop(“selected”, true); |
まず、jQuery 1.6では、windowとdocumentは、属性を持つことはできないので、windowとdocument上で.attr()メソッドを動かすこと はできなかった。それらは、.prop()か、単に生のjavascriptで操作されるべき(locationやreadyStateといった)プロパ ティを含んでいる。jQuery 1.6.1では、.attr()は、windowとdocumentの両方でエラーを投げるというという点で、.prop()メソッドとは異なる
First, using the .attr() method on the window or document did not work in jQuery 1.6 because the window and document cannot have attributes. They contain properties (such as location or readyState) that should be manipulated with .prop() or simply with raw javascript. In jQuery 1.6.1, the .attr() will defer to the .prop() method for both the window and document instead of throwing an error.
次に、前に言及したchecked, selected、その他の真偽属性は、これらの属性と対応するプロパティとの特別な関係のために特別扱いされる。基本的に、属性は、以下のようにHTMLでみられるようなものである。
Next, checked, selected, and other boolean attributes previously mentioned are receiving special treatment because of the special relationship between these attributes and their corresponding properties. Basically, an attribute is what you see in the html:
<input type=”checkbox” checked=”checked”>
checkedのような真偽属性は、デフォルトもしくは初期値のみセットされる。例えば、checkboxの場合、checked属性はページがロードされる時のチェックボックスのチェック状態をセットする。
Boolean attributes such as checked only set the default or initial value. In the case of a checkbox, the checked attribute sets whether the checkbox should be checked when the page loads.
プロパティは、ブラウザが監視している現在の値である。普通、プロパティは、(もし存在するならば)それに対応する値が反映される。これは真偽 属性の場合とは違う。真偽プロパティは、ユーザーがセレクト要素でチェックボックスや選択肢をクリックしたときに最新の状態になる。真偽属性の場合はそう はならない。上で述べたように、ブラウザによってセットされた初期値が使われるからだ。
Properties are what the browser uses to keep track of the current values. Normally, properties reflect their corresponding attributes (if they exist). This is not the case with boolean attributes. Boolean properties stay up to date when the user clicks a checkbox or selects an option in a select element. The corresponding boolean attributes do not. As was stated above, they are used by the browser only to store the initial value.
$(“:checkbox”).get(0).checked = true;// Is the same as $(":checkbox:first").prop(“checked”, true);
jQuery 1.6では、checkedを以下のように設定することは、
$(“:checkbox”).attr(“checked”, true);
実際にチェックボックスをチェックするというわけではない。というのは、これは設定されるプロパティであって、設定したものは全て初期値だからだ。
In jQuery 1.6, setting checked with
$(“:checkbox”).attr(“checked”, true);
would not check the checkbox because it was the property that needed to be set and all you were setting was the initial value.
しかしながら、jQuery1.6がリリースされると、jQueryチームは、ブラウザが、ページの読み込み時にのみブラウザが考慮するものを 設定することは、特に有用ではないと理解した。したがって、後方互換性と.attr()メソッドの利便性のために、1.6.1においても.attr()メ ソッドで真偽属性を取得、設定をできるようにした。
However, once jQuery 1.6 was released, the jQuery team understood that it was not particularly useful to set something that the browser was only concerned with on page load. Therefore, in the interest of backwards compatibility and the usefulness of the .attr() method, we will continue to be able to get and set these boolean attributes with the .attr() method in jQuery 1.6.1.
一番、一般的な真偽属性は、checked, selected, disabled, readOnlyだが、jQuery1.6の.attr()で動的に取得設定をサポートする真偽属性/プロパティの完全なリスト作った。
The most common boolean attributes are checked, selected, disabled, and readOnly, but here is a full list of boolean attributes/properties that jQuery 1.6.1 supports dynamically getting and setting with .attr():
autofocus, autoplay, async, checked, controls, defer, disabled,hidden, loop, multiple, open, readonly, required, scoped, selected
これらの真偽属性や真偽プロパティを設定する時は、.prop()が推奨される点に変わりはないが、.prop()メソッドへとコードの変更をしなくとも、jQuery1.6.1においてはコードを動かし続けることができる。
It is still recommended that .prop() be used when setting these boolean attributes/properties, but your code will continue working in jQuery 1.6.1 even if these use cases are not switched to use the .prop() method.
以下は、いくつかの属性とプロパティと、どのメソッドがそれらの取得/設定時に普通使われるかのリストである。これが、好ましい使い方ではあるのだが、.attr()メソッドも全ての属性においてきちんと動作する。
Below is a list of some attributes and properties and which method should normally be used when getting or setting them. This is the preferred usage, but the .attr() method will work in all attribute cases.
いくつかのDOM要素プロパティも、以下のリストにあるが、新しい.prop()メソッドでしか動かないことに注意して欲しい。
Note that some DOM Element properties are also listed below – but will only work with the new .prop() method.
| 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 ** |
✓ |
|
* 例えば、window.location
** If needed over .width()
.attr()、.prop()はどちらも値を取得/設定するために使われるべきではない。.val()メソッドを代わりに使うべきである。(1.6以前のように、.attr(“value”, “somevalue”)でも動くのだが・・・。).
Neither .attr() nor .prop() should be used for getting/setting value. Use the .val() method instead (although using .attr(“value”, “somevalue”) will continue to work, as it did before 1.6).
好ましい用法のまとめ
.prop()メソッドは、真偽属性/プロパティと(window.location)のようなhtmlに存在しないプロパティにつかわれるべきである。他の属性(つまり 、html内に見えるもの)は全て、 .attr()メソッドを使って操作できるし、そうすべきである。
The .prop() method should be used for boolean attributes/properties and for properties which do not exist in html (such as window.location). All other attributes (ones you can see in the html) can and should continue to be manipulated with the .attr() method.