フィルタ
Traversing | jQuery API Documentation
いきなり、Traversingの意味が解からない。
考察?
ADD20080128
http://tdweb.cssa.chs.nihon-u.ac.jp/ds/ds08.htmlやっと意味がわかった。
eq
何番目かを指定する。
指定した要素の子要素のindex*1が一致するjQueryオブジェクトをひとつだけ返す
下の書き方だと class="sample の中にある 要素(タグ)の 順番(index)を指定することが出来る。
書き方
jQuery(".sample").eq(2).addClass("strong");
hasclass
クラスを指定する
戻り値は、bool
指定した要素の中で、特定のクラスに属するものを指定する。
書き方
jQuery("div").click(function(){ if ( jQuery(this).hasClass("protected") ) jQuery(this).animate({ left: -10 }, 75) .animate({ left: 10 }, 75) .animate({ left: -10 }, 75) .animate({ left: 10 }, 75) .animate({ left: 0 }, 75); });
上のスクリプトの意味
divをクリックした時に、protectedというクラスに属していたら、アニメーションイベントを発生させて、divをブルブル左右に振るわせなさい。
(昔流行した、ボタンクリックすると震えるflashみたいな感じ)
.hasClass() | jQuery API Documentation
filter
処理を追加する
処理しているオブジェクトに、filter(式)を使って指定する。
$("#sample-filter *").css("background", "yellow") .filter(".middle").css("border-color", "#aaf");
is
is(式)を使って条件に合うものを指定する。
bool
細かい指定が出来そう
$("#sample-is div").one('click', function () { if ($(this).is(":first-child")) { $("#comment").text("そうです最初の子要素です。"); } else { $("#comment").text("最初の子要素では、ありません。"); } $("#comment").hide().slideDown("slow"); $(this).css({"border-style": "inset", cursor:"default"}); });
意味、クリックしたdivが、指定したdivの最初の要素だったら、#commentにメッセージを表示する。
map
指定した要素それぞれに、なんか処理をする。
map(式) は、取得した、domにコールバック関数の結果を適用します
$("#sample-map").append( $("#sample-map input").map(function(){ return "<h3>" + $(this).val() + "</h3>"; }).get().join("\n") ); <div id="map"> <div id="sample-map"> <input type="text" id="mame" value="banzan" /> <input type="text" id="age" value="106" /> <br /> </div> </div>
意味
テキストフィールドの値を取得して、sample-mapに追加して表示する。
.map() | jQuery API Documentation
not
わがままな指定
それじゃなく、アレじゃなく、そっちでもなく、こっちでもなく、
$("#sample-not *").not(".green, #blueone").css({"border-color":"red","border-width":"4px;"});
そういう人、あなたの近くにもいるでしょ。
slice
わかんない。
たぶん、eqは、ひとつの番号しか指定できないけど、indexの範囲を指定できるということだと思うけど、
まだやってみていないので、、、
後でやります。
*1:indexは、上から順に、0から始まる。