2011-09-15 18 views
0

ajaxを使用してページ(example.html)を読み込みます。 2つのボタンがあります:1つはajaxロード機能用で、もう1つはロードされたコンテンツに作用します。しかし、それは反応しません。私が使用してみました:jquery ajaxロードされた要素を選択して使用する方法

$(document).ready(function(){ 
    $("#load") 
     .click(function(){ 
     $("content").load("example.html"); 
    }); 
     $("#example_content").load(function(){ 
        // some actions to loaded page 
}); 

答えて

3

jQueryのload関数は次のように動作し、イベントフック関数なので、二.LOADコールが取得するために、サーバーへの新しい要求をするために、URLのような文字列を受け取ることを期待しますしません。より多くのデータ(リンクhttp://api.jquery.com/load/)。

あなたはdiv要素にロードされたコンテンツで何かをしたい場合は、私はあなたがこのように使用することができAjaxの方法、使用することをお勧めしたい:あなたは短い道にしたい場合は

$.ajax({ 

    //where is the data comming form? url 
    url : "example.html", 

    //what happens when the load was completed? the success function is called 
    success : function(data){ 
     //add the content from the html to the div 
     $("content").html(data.responseText); 

     //do whatever else I want with the content that was just added to 'content' 
     console.debug($('content')); // should show you all the html 
            // content written into that element 

     //my content specific funciton 
     myFunction(); 
    } 
}); 

を$ .get(url、success)関数を使用すると助けになるでしょうが、これは$ .ajaxの内部を使用するので、$ .ajaxを直接使用する方が良いでしょう。

要約:

1).LOADは、コンテンツを取得するために.getを使用する関数です。 http://api.jquery.com/load/

2).getには、指定されたURLから受け取ったコンテンツをターゲット要素に書き込むsuccess関数があります。 http://api.jquery.com/jQuery.get/

3).getは、jQueryのajax機能の中核をなす関数.ajaxです。 http://api.jquery.com/jQuery.ajax/

+0

ありがとうございました – djayii

関連する問題