[AJAX] Charger du contenu dans un élément cliqué
Bonjour,
Je découvre le javascript et je suis confronté à un problème simple qui dépasse néanmoins mes compétences. Je souhaite inclure le script suivant sur un site afin de charger dynamiquement du contenu dans une nouvelle page. Ce script fonctionne mais au lieu de charger le contenu dans un élément déterminé "en dur" j'aimerais récupérer l'id de l'élément cliqué précédemment pour y inclure les nouvelles données.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| $(function() {
if(Modernizr.history){
var newHash = "",
$mainContent = $("#main-content"),
$pageWrap = $("#page-wrap"),
baseHeight = 0,
$el;
$pageWrap.height($pageWrap.height());
baseHeight = $pageWrap.height() - $mainContent.height();
$("nav").delegate("a", "click", function() {
_link = $(this).attr("href");
history.pushState(null, null, _link);
loadContent(_link);
return false;
});
function loadContent(href){
$mainContent
.find("#guts")
.fadeOut(200, function() {
$mainContent.hide().load(href + " #guts", function() {
$mainContent.fadeIn(200, function() {
$pageWrap.animate({
height: baseHeight + $mainContent.height() + "px"
});
});
$("nav a").removeClass("current");
console.log(href);
$("nav a[href$="+href+"]").addClass("current");
});
});
}
$(window).bind('popstate', function(){
_link = location.pathname.replace(/^.*[\\\/]/, ''); //get filename only
loadContent(_link);
});
} // otherwise, history is not supported, so nothing fancy here.
}); |
J'ai tenté, sans succès, de compiler le script précédant avec le suivant pour remplacer #guts par l'id de l'élément cliqué.
Code:
1 2 3
| $("#a").click(function() {
var id = this.id;
}); |
Sauriez-vous m'éclairer ?
Merci !