【发布时间】:2010-07-22 06:44:08
【问题描述】:
我正在尝试使用锚的标题元素来确定目标通过 ajax 加载到哪个块元素中,目前它看起来像这样:
$(document).ready(function() {
$('#content').load('overview.php'); //by default initally load text from overview.php
$('body').delegate('a.ajax', 'click', function(e) { //start function when any link is clicked
e.preventDefault();
var content_show = $(this).attr("href"); //retrieve href of link so we can load the correct file
$.ajax( {
method: "get", url: content_show,data: 0,
beforeSend: function(){$("#loading").show("fast");}, //show loading just when link is clicked
complete: function(){ $("#loading").hide("fast");}, //stop showing loading when the process is complete
success: function(html) { //so, if data is retrieved, store it in html
$($(this).attr("title")).show("slow"); //animation
$($(this).attr("title")).html(html); //show the html inside the target div
}
}); //close $.ajax(
}); //close delegate(
}); //close $(
这样做的重点是,我可以在内容 div 中加载标题为 #content 的页面上单击的链接,并将标题设置为 #subcontent 的链接加载到子内容 div 中。这可能吗,如果是,我做错了什么?
更新 好的,显然 $(this) 变量在进入 $.ajax 调用时发生了变化,因此通过将 $(this).attr("title") 放入变量中,然后调用该变量代替选择器,它按预期工作。
【问题讨论】: