【发布时间】:2009-12-07 11:14:12
【问题描述】:
我有一系列链接,每个链接都有自己的 id:
<li><a id="about" href="#">About</a></li>
<li><a id="call-us" href="#">Contact us</a></li>
<li><a id="home" href="#">Head on home</a></li>
而不是像这样写一系列语句:
$(document).ready(function(){
$("a.about").click(function(event){
$("#content").load("/data.html #about");
});
$("a.call-us").click(function(event){
$("#content").load("/data.html #call-us");
});
});
有没有办法抽象出这样一个事实,即当我单击带有“ajax”类的链接时,我会使用它的#ID,查看.data.html,然后从data.html 拉出带有id 的div #ID?
(我现在明白这并没有指定类,但假设我有 5 个锚点,例如
<a href="#" class="ajax" id="UNIQUE-ID">Anchor Text</a>
它肯定会使代码更易于维护。任何输入都会很棒。
【问题讨论】:
-
我觉得我的回答太简单了,是我误解了你的部分问题吗?
-
嗯,有时人们会问直截了当的问题 :) 我现在正在测试,但很快就会接受。谢谢!