- 索引
-
- ├ 目次
- ├ jQuery(selector)
- ├ jQuery(html)
- ├ jQuery(function)
- ├ jQuery.holdReady()
- ├ jQuery.noConflict()
- └ jQuery.sub()
- 他ページ参照
- └ jQuery.when()
-
- └ 目次
- 基礎
- ├ *(すべて)
- ├ element(html要素)
- ├ #id名(ID属性)
- ├ .class名(clas属性)
- ├ 複数のセレクタ(and)
- └ 複数のセレクタ(or)
- 階層関連
- ├ 先祖 子孫
- ├ >(子要素)
- ├ +(直近の後要素)
- └ ~(後要素)
- 属性
- ├ [属性名]
- ├ [属性名='値']
- ├ [属性名!='値']
- ├ [属性名^='値']
- ├ [属性名$='値']
- ├ [属性名*='値']
- ├ [属性名-='値']
- ├ [属性名|='値']
- └ 複数の属性
- 基本フィルタ
- ├ :root
- ├ :header
- ├ :lang()
- ├ :target
- ├ :animated
- ├ :first
- ├ :last
- ├ :even
- ├ :odd
- ├ :eq()
- ├ :gt()
- ├ :lt()
- └ :not()
- 子要素フィルタ
- ├ :first-child
- ├ :first-of-type
- ├ :last-child
- ├ :last-of-type
- ├ :only-child
- ├ :only-of-type
- ├ :nth-child()
- ├ :nth-of-type()
- ├ :nth-last-child()
- └ :nth-last-of-type()
- 可視性フィルタ
- ├
- └ :visible
- コンテンツフィルタ
- ├ :contains()
- ├ :has()
- ├ :empty
- └ :parent
- フォーム
- ├ :checked
- ├ :selected
- ├ :disabled
- ├ :enabled
- ├ :focus
- ├
- ├ :checkbox
- ├ :file
- ├ :image
- ├ :input
- ├ :password
- ├ :radio
- ├ :reset
- ├ :submit
- └ :text
-
- ├ .目次
- ├ .hasClass()
- ├ .addClass()
- ├ .removeClass()
- ├ .toggleClass()
- ├ .attr()
- ├ .removeAttr()
- ├ .prop()
- ├ .removeProp()
- └ .val()
-
- └ 目次
- ツリー(階層)
- ├ .siblings()
- ├ .next()
- ├ .nextAll()
- ├ .nextUntil()
- ├ .prev()
- ├ .prevAll()
- ├ .prevUntil()
- ├ .children()
- ├ .find()
- ├ .parent()
- ├ .parents()
- ├ .parentsUntil()
- ├ .offsetParent()
- └ .closest()
- フィルタリング
- ├ .eq()
- ├ .slice()
- ├ .first()
- ├ .last()
- ├ .filter()
- ├ .has()
- ├ .not()
- ├ .is()
- └ .map()
- その他
- ├ .add()
- ├ .andSelf()
- ├ .addBack()
- ├ .end()
- ├ .contents()
- └ .each()
-
- ├ 目次
- 要素の挿入
- ├ .after()
- ├ .before()
- ├ .append()
- ├ .prepend()
- ├ .insertAfter()
- ├ .insertBefore()
- ├ .appendTo()
- └ .prependTo()
- 要素の削除
- ├ .empty()
- └ .remove()
- ├ .detach()
- 要素を囲む
- ├ .wrap()
- ├ .wrapAll()
- └ .wrapInner()
- ├ .unwrap()
- その他
- ├ .text()
- ├ .html()
- ├ .clone()
- ├ .replaceAll()
- └ .replaceWith()
- 他ページ参照
- ├ .toggleClass()
- ├ .hasClass()
- ├ .addClass()
- ├ .removeClass()
- ├ .attr()
- ├ .removeAttr()
- ├ .prop()
- ├ .removeProp()
- ├ .val()
- ├ .css()
- ├ .offset()
- ├ .position()
- ├ .height()
- ├ .innerHeight()
- ├ .outerHeight()
- ├ .width()
- ├ .innerWidth()
- ├ .outerWidth()
- ├ .scrollLeft()
- └ .scrollTop()
-
- ├ 目次
- ├ jQuery.cssHooks
- ├ .css()
- ├ .height()
- ├ .innerHeight()
- ├ .outerHeight()
- ├ .width()
- ├ .innerWidth()
- ├ .outerWidth()
- ├ .offset()
- ├ .position()
- ├ .scrollTop()
- └ .scrollLeft()
- 他ページ参照
- ├ .addClass()
- ├ .hasClass()
- ├ .removeClass()
- └ .toggleClass()
-
- └ 目次
- マウスイベント
- ├ .click()
- ├ .dblclick()
- ├ .mousedown()
- ├ .mouseup()
- ├ .mouseover()
- ├ .mouseout()
- ├ .mouseenter()
- ├ .mouseleave()
- ├ .mousemove()
- ├ .hover()
- └ .toggle()
- キーイベント
- ├ .keydown()
- ├ .keypress()
- ├ .keyup()
- ├ .focusin()
- └ .focusout()
- フォームイベント
- ├ .focus()
- ├ .blur()
- ├ .change()
- ├ .select()
- └ .submit()
- ブラウザイベント
- ├ .resize()
- ├ .scroll()
- └ .error()
- ロードイベント
- ├ .load()
- ├ .unload()
- └ .ready()
- イベントハンドラアタッチ
- ├ .bind()
- ├ .unbind()
- ├ .live()
- ├ .die()
- ├ .delegate()
- ├ .undelegate()
- ├ .on()
- ├ .off()
- ├ .one()
- ├ .triggerHandler()
- └ .trigger()
- イベントオブジェクト
- ├ jQuery.Event
- ├ eo.type
- ├ eo.data
- ├ eo.pageX
- ├ eo.pageY
- ├ eo.timeStamp
- ├ eo.which
- ├ eo.result
- ├ eo.namespace
- ├ eo.target
- ├ eo.currentTarget
- ├ eo.delegateTarget
- ├
- ├ eo.stopPropagation()
- ├ eo.stopImmediateP...
- ├ eo.preventDefault()
- ├ eo.isPropagationSt...
- ├ eo.isImmediatePre...
- └ eo.isDefaultPreven...
-
- └ 目次
- 基礎
- ├ .hide()
- ├ .show()
- └ .toggle()
- スライド
- ├ .slideUp()
- ├ .slideDown()
- └ .slideToggle()
- フェード
- ├ .fadeOut()
- ├ .fadeIn()
- ├ .fadeTo()
- └ .fadeToggle()
- カスタム
- ├ .animate(prop)
- ├ .animate(prop,opt...
- ├ .stop()
- ├ .delay()
- ├ .queue()
- ├ .dequeue()
- ├ .clearQueue()
- ├ .finish()
- ├ jQuery.queue()
- ├ jQuery.dequeue()
- ├ jQuery.fx.interval
- └ jQuery.fx.off
-
- └ 目次
- 簡易メソッド
- ├ .load()
- ├ jQuery.get()
- ├ jQuery.getJSON()
- ├ jQuery.getScript()
- └ jQuery.post()
- グローバルイベントハンドラ
- ├ .ajaxComplete()
- ├ .ajaxSuccess()
- ├ .ajaxError()
- ├ .ajaxSend()
- ├ .ajaxStart()
- └ .ajaxStop()
- 低レベル・インターフェイス
- ├ jQuery.ajax()
- ├ jQuery.ajaxSetup()
- └ jQuery.ajaxPrefilt...
- お助け機能
- ├ jQuery.param()
- ├ .serialize()
- └ .serializeArray()
-
- └ 目次
- チェック
- ├ jQuery.contains()
- ├ jQuery.isArray()
- ├ jQuery.isEmptyO...
- ├ jQuery.isPlainO...
- ├ jQuery.isNumeric()
- ├ jQuery.isFunction()
- ├ jQuery.isWindow()
- └ jQuery.isXMLDoc()
- 配列/Object関連
- ├ jQuery.makeArray()
- ├ jQuery.inArray()
- ├ jQuery.grep()
- ├ jQuery.each()
- ├ jQuery.map()
- ├ jQuery.merge()
- ├ jQuery.extend()
- └ jQuery.unique()
- その他
- ├ jQuery.globalEval()
- ├ jQuery.noop()
- ├ jQuery.now()
- ├ jQuery.parseHTML()
- ├ jQuery.parseJSON
- ├ jQuery.parseXML()
- ├ jQuery.proxy()
- ├ jQuery.trim()
- ├ jQuery.type()
- ├ jQuery.support
- ├ jQuery.boxModel
- └ jQuery.browser
- 他ページ参照
- ├ .queue()
- ├ .dequeue()
- ├ .clearQueue()
- ├ jQuery.queue()
- ├ jQuery.dequeue()
- ├ jQuery.data()
- └ jQuery.removeData()
-
- ├ 目次
- ├ .data()
- ├ .removeData()
- ├ jQuery.data()
- ├ jQuery.removeData()
- └ jQuery.hasData()
- 他ページ参照
- ├ .queue()
- ├ .dequeue()
- ├ .clearQueue()
- ├ jQuery.queue()
- └ jQuery.dequeue()
-
- └ 目次
- deferredの作成
- └ jQuery.deferred()
- promiseの作成
- ├ jQuery.when()
- ├ .promise()
- └ deferred.promise()
- コールバックの設定
- ├ deferred.done()
- ├ deferred.fail()
- ├ deferred.always()
- ├ deferred.progress()
- ├ deferred.then()
- └ deferred.pipe()
- 状態の取得
- ├ deferred.state()
- ├ deferred.isReso...
- └ deferred.isReje...
- 状態の変更
- ├ deferred.resolve()
- ├ deferred.resolveW...
- ├ deferred.reject()
- ├ deferred.rejectW...
- ├ deferred.notify()
- └ deferred.notifyW...
-
- ├ 目次
- ├ jQuery.error()
- ├ .context
- ├ .jquery
- └ .pushStack()
jQuery.proxy( )thisの内容を変更
構文
- thisの内容を変更返値:function
-
jQuery.proxy( function, thisとするもの )ver1.4〜
jQuery.proxy( thisとするもの, function名 )ver1.4〜
機能
イベントハンドラ内のthisはイベントが発生した要素を指します。しかしjQuery.proxyメソッドを利用するとfunction内で利用するthisを自由に設定することができます。
解説
まずはjQuery.proxyメソッドを利用しないサンプル
サンプル(proxy/01.html)を開いてbody内にbutton要素しかないことを確認して下さい。このbutton要素にはname属性で「button」と設定されていることを覚えておいて下さい。
<button name="button">click</button>
jQueryでは、まず以下の部分を確認してください。新規にobjectオブジェクト「johnObj」を作成し、nameプロパティに「john」と設定しています。さらにfunction「myFunc」を設定しました。このfunctionの処理内容はthisに設定されたnameプロパティをアラートで表示するだけです。この処理によってthisが何を指すかを確認します。
var johnObj = new Object(); johnObj.name = "john"; johnObj.myFunc = function (){ alert(this.name); };
続いて以下の部分を確認してください。buttonをクリックしたらjohnObjに設定したmyFuncを実行します。結果としては「john」とアラートで表示されます。
$("button").click(function (){ johnObj.myFunc(); });
このサンプルで確認できるように、通常thisはfunctionが設定されているオブジェクトを指します。しかし初心者にとって混乱するケースがあるので次項で紹介します。
イベントに直接functionを設定すると...
サンプルproxy/01.htmlのbuttonをクリックする処理は、すこし回りくどい感じがします。サンプル(proxy/02.html)を開いてbuttonをクリックする処理を確認して下さい。以下の様にclickの引数に直接johnObj.myFuncを設定しました。
$("button").click(johnObj.myFunc);
しかしbuttonをクリックすると、proxy/01.htmlとは異なり「button」とアラートで表示されます。
初心者にとってややこしいと感じるかもしれませんが、上記のような記述だと「button要素のclickのイベントハンドラとしてjohnObjにあるmyFuncをコピーして貼り付ける」。という意味になります。そのためjohnObjのmyFuncが実行されるのではなく、button要素にコピーされたmyFuncを実行していることになるのです。ですからアラートでbutton要素のname属性の値「button」が表示されるのです。しかしjQuery.proxyメソッドを利用すると、この様なケースでもthisの内容を自由に設定することができます。
thisの内容を変更する
サンプル(proxy/03.html)を開いて、proxy/02.htmlとほとんど同じ事を確認して下さい。異なるのは以下の様にclickの引数にjQuery.proxyメソッドを利用している点だけです。
$(function () { $("button").click($.proxy(johnObj.myFunc, johnObj)); });
jQuery.proxyメソッドの第1引数には実行したいfunctionをオブジェクト付きで設定します。サンプルではjohnObjのmyFuncを実行したいので「johnObj.myFunc」と設定してあります。そして第2引数でfunction内のthisの内容を設定します。サンプルではjohnObjを設定しました。
結果として、buttonをクリックするとmyFunc内のthisはjohnObjとなるため、johnObjのname属性の値である「john」がアラートで表示されます。
もう1つの構文
thisとしたいオブジェクトに設定されたfunctionを実行する場合は、「構文」の2段目にある構文が利用できます。サンプル(proxy/04.html)を開いて、jQuery.proxyメソッドが以下の様に変更されているのを確認して下さい。第1引数にthisとして設定したいオブジェクト、第2引数に第1引数に設定されている実行したいfunction名を文字列で設定しています。
$(function () { $("button").click($.proxy(johnObj, "myFunc")); });
この構文でもproxy/03.htmlと同じようにthisをjohnObjに設定できるので、buttonをクリックすると「john」とアラートで表示されます。thisとしたいオブジェクトに設定されたfunctionを実行する場合は、こちらの方を利用した方が分かりやすいでしょう。
メモ
いまいちメリットが分からない
jQuery.proxyメソッドを利用するとthisを自由に変更できるため、functionで利用する対象を変更することが可能となります。サンプル(proxy/test01.html)を開いて以下の様に3種類のobjectオブジェクトが設定され、それぞれnameプロパティとpriceプロパティが設定されているのを確認して下さい。
var appleObj = new Object(); appleObj.name = "apple"; appleObj.price = 100; var orangeObj = new Object(); orangeObj.name = "orange"; orangeObj.price = 80; var bananaObj = new Object(); bananaObj.name = "banana"; bananaObj.price = 40;
続いて以下の処理を確認して下さい。このfunctionでthisの内容を変更できれば、これ1つで複数のobjectオブジェクトの操作ができます。
function fruitFunc(){ var myStr = this.name + "の値段は" + this.price; $("div").text(myStr) }
実際、thisはjQuery.proxyメソッドで変更できますから、以下の様にボタンごとにjQuery.proxyメソッドの引数を変更すれば良いだけです。これでbuttonをクリックすると、ボタンに対応した果物のデータが表示されます。
$("#apple").click($.proxy(fruitFunc, appleObj)); $("#orange").click($.proxy(fruitFunc, orangeObj)); $("#banana").click($.proxy(fruitFunc, bananaObj));
便利そうに見えるかもしれませんが、大抵のケースではjQuery.proxyメソッドを利用しなくても同じようなことができる気がします。今回のケースでもclickイベントは以下の様に第1引数にはオブジェクトが設定できますからサンプル(proxy/test01b.html)のようにシンプルに記述できます。
$("#apple").click(appleObj, fruitFunc); $("#orange").click(orangeObj, fruitFunc); $("#banana").click(bananaObj, fruitFunc);
第1引数で渡されたオブジェクトはイベントオブジェクトのdataプロパティ経由で渡されますから、fruitFuncは以下の様に変更しています。
function fruitFunc(eo){ var myStr = eo.data.name + "の値段は" + eo.data.price; $("div").text(myStr) };
結果として、proxy/test01b.htmlもproxy/test01.htmlとまったく同じように動作します。
次のページ: jQuery.trim()