2010-08-19 15 views
1

Comment faire fonctionner ce code? Je ne vois pas comment je peux atteindre div à partir de $.get rappel.jquery: récupère l'élément parent de l'appel ajax

$("<div/>", { 
    text: "some text", 
    click: function (e) { 
     $.get("bar.php", function(data) { 
      $(this).text(data); // doesn't work 
    }); 
    } 
}).appendTo("body"); 

Répondre

5

Créer une variable dans le gestionnaire de clic qui maintient la référence à la DIV et utiliser cette variable dans la fonction de rappel .get $.

$("<div/>", { 
    text: "some text", 
    click: function (e) { 
     var $div = $(this); 
     $.get("bar.php", function(data) { 
      $div.text(data); // should work 
    }); 
    } 
}).appendTo("body");