HTML5が巨人の川相なら、data-属性はHTML5の川相
意味不明なタイトルですんません。増田にインスパイアされました。
さて HTML5 界隈が騒がしい今日このごろですが、data- 属性というものを皆さんご存知でしょうか?
Twitter のツイートボタン作成ページで作った HTML に入ってる data-via とかのことです。最近色んなところでよく目にしますが、これを使うと任意の DOM 要素に任意の値をセットできるみたいです。
で、この data- 属性なんですが HTML5 の仕様の一部だったみたいです。恥ずかしながらちゃんとした仕様だということを最近知ったのですが、個人的には canvas とか video とかをブッちぎって一番重要な HTML4 からの変更点じゃないかと思います。
てことで本日は、努力家だけど恥ずかしがり屋さんの data- 属性さん(46歳)が実はいぶし銀のスーパースターだということをプロバガンダをしようと思います。
data- 属性は HTML5 の規格の一部
まず超基本的なことですが、data- 属性は HTML5 の規格の一部です。
W3C の HTML5 draft の 3.2.3.8 Embedding custom non-visible data with the data-* attributes に定義されています。
Custom data attributes are intended to store custom data private to the page or application, for which there are no more appropriate attributes or elements.
3.2.3.8 Embedding custom non-visible data with the data-* attributes
W3C では data- 属性のことを Custom data attributes と呼んでいるようです。日本語では独自データ属性と表記しているサイトが多いと思います(独自データ属性 – グローバル属性 – HTML5 タグリファレンス – HTML5.JP とか)。
重要なのは↓の部分。(英語の仕様を貼っててもアレなんで HTML5.JP さんから引用)
すべての HTML 要素に対して、独自データ属性をいくつでも、どんな値でも指定することができます。
独自データ属性 – グローバル属性 – HTML5 タグリファレンス – HTML5.JP
どの要素に何個でも付けれるらしいです(´・ω・`)便利だねー
てことで HTML5 の独自データ属性を使うと、meta タグとか(Twitter がやってた)、type=”hidden” の input タグとか(form の下位要素じゃないと HTML 的にまずいような)、display:none にした span とか div とか(なんだかねーって感じ)、id や class 属性とか(属性値に制限あり)に、無理やりセットしてたデータをスマートにコーディングできるようになります。
使い方
まず HTML は以下のように書きます。data- に続けてアルファベットと – で任意のキー名を指定します。
data-weapons="laser 2" data-shields="50%"
data-x="30" data-y="10" data-z="90">宇宙船ちきう号</div>
DOM を使って JavaScript から値を参照する場合は以下のようにコーディングします。
if (spaceship.dataset) {
alert(spaceship.dataset.weapons);
alert(spaceship.dataset.shipId);
} else {
alert(spaceship.getAttribute("data-weapons"));
alert(spaceship.getAttribute("data-ship-id"));
}
上のコードでは dataset プロパティが存在するかチェックして dataset に対応していないブラウザをフォールバックしています。手元の Firefox4 が dataset プロパティに対応していないようでしたので、実際にコーディングする際は getAttribute だけで良いような気もします。
dataset プロパティを使用する場合は、キーに – が含まれている場合は Camel にします。(data-ship-id → dataset.shipId)
値をセットする場合は dataset プロパティに代入するか setAttribute を使います。jQuery を使った方が便利ですのでこの辺の例は省略、ってことで興味ある方はご自分でお調べください。
jQuery の場合
jQuery には $.data() と .data() という似たような名前の関数がありまして、HTML5 の独自データ属性と対応するのは .data() の方なのでご注意ください。$.data() も .data() もプレースホルダを提供する関数なのですが、$.data() の方は完全に jQuery 独自の実装となっています。
で、.data() の方は大方の読者の予想の通り↓のようにコーディングできます。
alert($('#spaceship').data('ship-id'));
値をセットする場合はこんな感じ。
alert($('#spaceship').data('weapons'));
ちなみに、.data() で値をセットしても data- 属性には書き込まず $.cache に値がセットされます。data- 属性に書き込みたい場合は .attr() を使ってください。なんかややこい挙動ですが、なんか理由があってこーなってる気がします。(jQuery のサイトでなんで HTML5 の data- に書き込まないの?って感じのコメントしてる人がいますが華麗にスルーされてますし。)
また、.data() の引数を省略するとデータをオブジェクトで返します。
値にはオブジェクトも指定できます。
alert($('body').data('bar').myType);
ぶっちゃけ大して難しいところは無いと思いますので説明はこの辺で。
ということで、いぶし銀 JavaScripter を目指す皆様におかれましては data- 属性で千本バント練習に打ち込むのがよろしいかと思います。
Dibuja los pétalos de la flor trazando líneas curvas.
AMAZING WORK GUYS JUST KEEP IT UP !
Your new valuable key points imply much a person like
me and extremely more to my office workers. With thanks;
from everyone of us.
They have truly been a life-changing experience and I couldn’t have asked for better treatment.
My experience has changed me for the better and gives you everything you need to make a better life on the outside.
With the therapist’s help and professional advice you truly are given everything, you need to succeed in your life of sobriety.
Thanks to you all.
FB URL Shortener
[...]we came across a cool web page that you just may appreciate. Take a look should you want[...]
esports domain
[...]always a big fan of linking to bloggers that I like but really don’t get quite a bit of link like from[...]
clima en chimalhuacan
[...]check beneath, are some completely unrelated internet sites to ours, on the other hand, they’re most trustworthy sources that we use[...]
french bulldog
[...]that would be the finish of this report. Right here you will locate some web sites that we consider you値l value, just click the hyperlinks over[...]
dog registry
[...]Every as soon as inside a while we pick blogs that we study. Listed beneath would be the most recent sites that we pick out [...]
french bulldog puppies near me
[...]below you値l come across the link to some web sites that we feel you’ll want to visit[...]
game ESP downloads
[...]we came across a cool internet site which you could possibly delight in. Take a look should you want[...]
aimbot marvel rivals
[...]below you will find the link to some websites that we assume you must visit[...]
securecheats bfv hacks
[...]that may be the end of this write-up. Here you値l find some websites that we assume you will enjoy, just click the links over[...]
farlight 84 mod
[...]that would be the end of this post. Here you will come across some sites that we assume you will appreciate, just click the hyperlinks over[...]
aimbot eft
[...]below you will find the link to some web-sites that we believe you should visit[...]
securecheats valorant hacks
[...]the time to read or visit the subject material or websites we have linked to below the[...]
mexican candy store
[...]Every once in a whilst we pick blogs that we study. Listed below are the most current web-sites that we select [...]
condiciones climaticas queretaro
[...]Wonderful story, reckoned we could combine a couple of unrelated information, nonetheless genuinely really worth taking a look, whoa did one find out about Mid East has got much more problerms as well [...]
isla mujeres golf cart rental
[...]we came across a cool internet site that you just may possibly take pleasure in. Take a look for those who want[...]
french bulldog puppies for sale $200
[...]we prefer to honor quite a few other internet internet sites around the web, even if they aren稚 linked to us, by linking to them. Underneath are some webpages really worth checking out[...]
in vitro fertilization mexico
[...]very handful of websites that happen to be detailed below, from our point of view are undoubtedly effectively really worth checking out[...]
linh hoang
[...]always a big fan of linking to bloggers that I adore but really don’t get quite a bit of link adore from[...]
french bulldog puppies for sale houston texas
[...]Here is a good Weblog You may Find Exciting that we Encourage You[...]
alexa collins
[...]just beneath, are quite a few absolutely not associated web pages to ours, on the other hand, they may be surely really worth going over[...]
늑대닷컴
[...]the time to study or go to the content material or web pages we’ve linked to below the[...]
family ho
[...]one of our visitors recently suggested the following website[...]
늑대닷컴
[...]one of our guests lately suggested the following website[...]
wix seo
[...]very few web-sites that happen to be comprehensive below, from our point of view are undoubtedly well really worth checking out[...]
dog probiotic on amazon
[...]the time to read or check out the content material or web-sites we’ve linked to below the[...]
fertility institute of nj
[...]Here is an excellent Weblog You might Find Exciting that we Encourage You[...]
dr kim acupuncture
[...]The data talked about in the report are a number of the top offered [...]
we buy french bulldogs
[...]please take a look at the internet sites we adhere to, including this one particular, because it represents our picks in the web[...]
mexican candy store near me
[...]check beneath, are some totally unrelated internet sites to ours, having said that, they may be most trustworthy sources that we use[...]
mexican candy store near me
[...]one of our visitors a short while ago encouraged the following website[...]
mexican candy store near me
[...]one of our visitors not long ago proposed the following website[...]
mexican candy store near me
[...]below you will come across the link to some internet sites that we assume you must visit[...]
mexican candy store near me
[...]the time to study or check out the material or sites we’ve linked to beneath the[...]
linh hoang
[...]Sites of interest we have a link to[...]
french bulldog puppies for sale in texas
[...]always a large fan of linking to bloggers that I love but don稚 get a good deal of link adore from[...]
best probiotic for french bulldogs
[...]The details talked about within the report are some of the top out there [...]
french bulldog usa
[...]usually posts some extremely intriguing stuff like this. If you池e new to this site[...]
brazil crop top
[...]Every as soon as inside a when we choose blogs that we study. Listed beneath would be the latest web sites that we choose [...]
coco chanel dog collar
[...]Here is an excellent Weblog You may Uncover Exciting that we Encourage You[...]
chanel dog bowls
[...]just beneath, are a lot of totally not related internet sites to ours, on the other hand, they’re surely really worth going over[...]
playnet
[...]please take a look at the web-sites we follow, such as this one particular, because it represents our picks in the web[...]
crypto news
[...]that could be the finish of this post. Right here you will obtain some internet sites that we believe you値l appreciate, just click the hyperlinks over[...]
bjj jiu jitsu magnolia texas
[...]The information talked about in the article are a few of the most beneficial out there [...]
frenchie bully mix
[...]please visit the websites we adhere to, including this one, because it represents our picks in the web[...]
frenchie yorkie mix
[...]always a big fan of linking to bloggers that I adore but really don’t get a whole lot of link love from[...]
boston terrier french bulldog mix
[...]Sites of interest we have a link to[...]
best canine probiotics for bullies
[...]check below, are some totally unrelated sites to ours, even so, they may be most trustworthy sources that we use[...]
probiotics for french bulldogs
[...]Here is an excellent Weblog You might Discover Interesting that we Encourage You[...]
How To Obtain Dog Papers
[...]below you will obtain the link to some internet sites that we believe you’ll want to visit[...]
Dog Breed Registries
[...]Sites of interest we have a link to[...]
How To Get My Dog Papers
[...]Wonderful story, reckoned we could combine a couple of unrelated data, nevertheless truly worth taking a look, whoa did 1 master about Mid East has got extra problerms too [...]
How To Obtain Dog Papers
[...]always a big fan of linking to bloggers that I appreciate but do not get a good deal of link adore from[...]
Dog Registry
[...]Here are some of the web-sites we advise for our visitors[...]
Dog Breed Registries
[...]although sites we backlink to below are considerably not related to ours, we feel they are actually really worth a go as a result of, so possess a look[...]
Dog Papers
[...]Every when inside a even though we pick out blogs that we study. Listed beneath are the newest web pages that we select [...]
Dog Papers
[...]just beneath, are several completely not related internet sites to ours, having said that, they’re certainly worth going over[...]
Dog Papers
[...]one of our guests a short while ago recommended the following website[...]
Dog Papers
[...]usually posts some quite interesting stuff like this. If you are new to this site[...]