[jQuery] ドットインストール「jQuery入門」のまとめ
memo.
[markdown]
## idやclassを指定してみよう
“`javascript
$(function() {
// セレクタ.メソッド
// $(”)
// html要素 p h1 ul
// id #main
// class .item
$(“.item”).css(‘color’, ‘red’);
});
“`
> * [#03 idやclassを指定してみよう | jQuery入門 – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_jquery_v2/25203)
## もっとセレクタを使ってみよう
“`javascript
“`
> * [#04 もっとセレクタを使ってみよう](http://dotinstall.com/lessons/basic_jquery_v2/25204)
## フィルタを使ってみよう
“`javascript
“`
> * [#05 フィルタを使ってみよう](http://dotinstall.com/lessons/basic_jquery_v2/25205)
## メソッドを使ったDOM要素指定
“`javascript
$(function() {
// parent(), children()
$(“#sub”).parent().css(‘color’, ‘red’);
// next(), prev()
$(“#sub > li:eq(2)”).next().css(‘color’, ‘red’);
// siblings()
$(“#sub > li:eq(2)”).siblings().css(‘color’, ‘red’);
});
“`
> * [#06 メソッドを使ったDOM要素指定 | jQuery入門 – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_jquery_v2/25206)
## 属性セレクタを使ってみよう
“`javascript
$(function() {
// セレクタ.メソッド
// 属性セレクタ
// = 同じ
// != ではない
// *= 含む
// ^= 先頭
// $= 末尾
$(‘a[href!=”http://google.com”]’).css(‘background’, ‘red’);
});
“`
> * [#07 属性セレクタを使ってみよう | jQuery入門 – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_jquery_v2/25207)
## css、addClass/removeClassを使おう
“`javascript
$(function() {
// セレクタ.メソッド
// .css 設定と取得
$(‘p’).css({
‘color’: ‘red’,
‘background’: ‘blue’
});
console.log($(‘p’).css(‘color’));
// addClass removeClass
$(‘p’).addClass(‘myStyle’);
});
“`
> * [#08 css、addClass/removeClassを使おう | jQuery入門 – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_jquery_v2/25208)
## attr、dataを使ってみよう
“`javascript
$(function() {
// attr 取得と設定
console.log($(‘a’).attr(‘href’));
$(‘a’).attr(‘href’, ‘http://google.co.jp’);
// data 取得
//
console.log($(‘a’).data(‘sitename’));
});
“`
> * [#09 attr、dataを使ってみよう | jQuery入門 – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_jquery_v2/25209)
## タグの中身を操作してみよう
“`javascript
$(function() {
// text
$(‘a:eq(0)’).text(‘日本語’);
// html
$(‘p’).html(‘click‘);
// val … get the value of form
console.log($(‘input’).val());
$(‘input’).val(‘hello!!!’);
// remove, empty
$(‘p’).empty();
$(‘p’).remove();
});
“`
> * [#10 タグの中身を操作してみよう | jQuery入門 – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_jquery_v2/25210)
## 要素を追加してみよう
“`javascript
$(function() {
var li = $(‘
// before, after -> insertBefore, insertAfter
$(‘ul > li:eq(1)’).before(li);
li.insertBefore($(‘ul > li:eq(1)’));
// prepend, append -> prependTo, appendTo
$(‘ul’).prepend(li);
li.prependTo($(‘ul’));
});
“`
> * [#11 要素を追加してみよう | jQuery入門 – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_jquery_v2/25211)
## エフェクトを使ってみよう
“`javascript
$(function() {
// hide, show
$(‘#box’).hide(800);
// fadeOut, fadeIn
$(‘#box’).fadeOut(800);
// toggle
$(‘#box’).toggle(800);
$(‘#box’).toggle(800);
$(‘#box’).toggle(800);
// Callback function
$(‘#box’).fadeOut(‘800’, function() {
alert(“gone!”);
});
});
“`
> * [#12 エフェクトを使ってみよう | jQuery入門 – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_jquery_v2/25212)
## イベントを使ってみよう
“`javascript
$(function() {
// click
$(‘#box’).click(function(event) {
alert(“hi!”);
});
// mouseover, mouseout, mousemove
$(‘#box’)
.mouseover(function(event) {
$(this).css(‘background’, ‘blue’);
})
.mouseout(function(event) {
$(this).css(‘background’, ‘red’);
})
.mousemove(function(event) {
$(this).text(event.pageX);
});
});
“`
> * [#13 イベントを使ってみよう | jQuery入門 – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_jquery_v2/25213)
## focus、blur、changeを使おう
“`javascript
$(function() {
// focus, blur
$(‘#name’)
.focus(function(event) {
$(this).css(‘background’, ‘red’);
})
.blur(function(event) {
$(this).css(‘background’, ‘white’);
});
// change
$(‘#members’).change(function(event) {
alert(‘Changed!’);
});
});
“`
> * [#14 focus、blur、changeを使おう | jQuery入門 – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_jquery_v2/25214)
## onメソッドを使ってみよう
“`javascript
$(function() {
// on
// ドキュメントが読み込まれた段階で存在しない要素を操作する
$(‘button’).click(function(event) {
var p = $(‘
‘).text(‘vanish!’).addClass(‘vanish’);
$(this).before(p);
});
/* 以下は .vanish が存在しないのでうまく動かない
$(‘.vanish’).click(function(event) {
$(this).remove();
});
*/
$(‘body’).on(‘click’, ‘.vanish’, function(event) {
$(this).remove();
});
});
“`
> * [#15 onメソッドを使ってみよう | jQuery入門 – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_jquery_v2/25215)
## loadメソッドを使ってみよう
Vagrant を利用。
* `ipconfig` … ipアドレスを調べる。
* `php -S ipアドレス:8000` … PHP の簡易サーバ
Mac は設定なしで利用可能。
* `ifconfig`
* `php -S ipアドレス:8000`
> * [Shorthand Methods | jQuery API Documentation](http://api.jquery.com/category/ajax/shorthand-methods/)
“`javascript
$(function() {
// load
$(‘button’).click(function(event) {
$(‘#result’).load(‘news/news.html’);
});
});
“`
> * [#17 loadメソッドを使ってみよう | jQuery入門 – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_jquery_v2/25217)
## 非同期通信とはなにか?
“`javascript
$(function() {
// Ajax
// Asynchronous JavaScript + XML
// サーバと通信 + ページの書き換え
// 非同期: 処理が終わる前に次の処理に移る
// .load
$(‘button’).click(function(event) {
// load 終了後に別処理を行うには、Callback 関数を使う
$(‘#result’).load(‘news/news.html’, function() {
$(‘#message’).css(‘color’, ‘red’);
});
});
});
“`
> * [#18 非同期通信とはなにか? | jQuery入門 – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_jquery_v2/25218)
## $.getを使ってみよう / JSONで値を返してみよう
“`javascript
$(function() {
// Ajax
// Asynchronous JavaScript + XML
// サーバと通信 + ページの書き換え
// 非同期: 処理が終わる前に次の処理に移る
// $.post
// $.get
$(‘#greet’).click(function(event) {
$.get(‘greet.php’, {
name: $(‘#name’).val()
}, function(data) {
$(‘#result’).html(data.message + ‘(‘ + data.length + ‘)’);
});
});
});
“`
“`php:greet.php
htmlspecialchars(“hi! ” . $_GET[‘name’], ENT_QUOTES, “utf-8”),
“length” => strlen($_GET[‘name’])
);
header(‘Content-Type: application/json; charset=utf-8’);
echo json_encode($rs);
“`
> * [#19 $.getを使ってみよう | jQuery入門 – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_jquery_v2/25219)
> * [#20 JSONで値を返してみよう | jQuery入門 – プログラミングならドットインストール](http://dotinstall.com/lessons/basic_jquery_v2/25220)
[/markdown]