jQueryまとめ
最近(今更だけど・・・)、jQueryよく使ってるのでメモ
ページの読み込み
- DOMツリーの構築が完了した時点で処理を実行する
- リソースを全て読み込むのを待たなくていい
$(function () { // 何かの処理 })
- リソースの読み込みが全て完了後
$(window).load(function() { // 何かの処理 })
セレクタ
- id
$('#hoge');
- class
$('.hoge');
- name
- select要素の名前が"hoge"の場合
$('select[name="hoge"');
- form
<input type="xxx">
で取れるものはこれでいけるぽい
$(':text'); $(':checkbox'); $(':radio'); $(':password'); $(':file');
要素
- 追加
<select id="test_select"></select>
が存在するとして- html()が表示される部分
- val()が選択されたときの値
$('#test_select').append('<option>').html('test').val(1);
ループ
$.each(new Array(10), function(i) { console.log(i + '回目') })
json
var data = [ {"no": 1, "name" : "foo"}, {"no": 2, "name" : "bar"}, {"no": 3, "name" : "hoge"} ] for (var i in data) { console.log('no ' + data[i].no); console.log('name ' + data[i].name); }