2011-07-21 13 views
2

"ajaxing"のため要素のinnerHTMLが変更されたときにイベントをキャッチしたいと思います。イベント、when(inner)htmlの変更

$('.t_error span').html.change(function(){ 
... 
}); 

おかげで助けを求める:

私はこのような何かを意味します!

+3

ajaxコールの成功関数で単に「イベント」をトリガーしないのはなぜですか? – BonyT

+0

私はこれとAjaxのものを分離したいと思います。 –

+2

何らかの理由がありますか?あなたは簡単にそれをすることはできないので、問題は疑問です。 HTMLコンテンツにChangeイベントはありません。そのため、イベントを書き込む必要があり、Ajaxコールからイベントをトリガーする必要があります。 – BonyT

答えて

0

あなたが求めるものを行うための方法はありません - しかし、代わりにあなたは、この構文を使用することができます。

$.ajax({ 
    type: "GET", 
    url: yourUrl:, 
    success: function (data) { 
     ajaxComplete(); 
    } 
}); 

function ajaxComplete() 
{ 
    .... 
} 

EDIT:あなたがしたい場合

:基本的にCustomEvent関数を定義

:賢くなるために、あなたはCustom Eventsを使用することができます

var CustomEvent = function() { 
    //name of the event 
    this.eventName = arguments[0]; 
    var mEventName = this.eventName; 

    //function to call on event fire 
    var eventAction = null; 

    //subscribe a function to the event 
    this.subscribe = function(fn) { 
    eventAction = fn; 
    }; 

    //fire the event 
    this.fire = function(sender, eventArgs) { 
    this.eventName = eventName2; 
    if(eventAction != null) { 
     eventAction(sender, eventArgs); 
    } 
    else { 
     alert('There was no function subscribed to the ' + mEventName + ' event!'); 
    } 
    }; 
}; 

次に、あなただけのどこかCustomEventクラスのインスタンスを作成する必要があります。あなたのページで

var myEvent = new CustomEvent("my event"); 

、それを購読する:

myEvent.subscribe(function(sender, eventArgs) { 
    alert(eventArgs.message); 
}); 

とあなたのAjax呼び出しの成功の機能では、トリガそれ:

$.ajax({ 
    type: "GET", 
    url: yourUrl:, 
    success: function (data) { 
     myEvent.fire(null, {message: 'you just witnessed the firing of a custom event called ' + this.eventName + '!'}); 
    } 
}); 
0

変更を監視するのではなく、単に機能をAJAXアップデートに組み込むだけではありません。ドキュメントのコールバックを参照してください:http://api.jquery.com/jQuery.ajax/

0

all modern browsersでは、MutationObserver APIを使用して構造任意のDOMノードまたはサブツリーの変更。あなたがそれらの変化が何であるかを気にしないで、ただ彼らの話を聞き、行動を起こさせたいなら、あなたが求めるものに似た非常に小さな図書館で手に入れることができます。 this JSFiddleで少しそれを試してみてください:

// a minimal jQuery library for reacting to innerHTML changes 
(function($) { 
    $.fn.change = function(cb, e) { 
    e = e || { subtree:true, childList:true, characterData:true }; 
    $(this).each(function() { 
     function callback(changes) { cb.call(node, changes, this); } 
     var node = this; 
     (new MutationObserver(callback)).observe(node, e); 
    }); 
    }; 
})(jQuery); 

使用法:$(elements).change(callback, optional_events)elementsは、あなたが気に要素(またはそのためのセレクタ)で、与えられたときcallbackは、あなたの関数とoptional_eventsで、プロパティを持つオブジェクトですW3C specを見てみましょう。典型的なjQueryイベントハンドラと同様に、コールバックのthisオブジェクトは何か起きたノードになります。記録されたすべての変更のリストを調べる場合、イベントハンドラは最初の引数としてそれらを取得します。観察者自身、それは第二の議論です。

関連する問題