如何简化这段 jQuery 代码?
<script>
$(document).ready(function () {
// for some reason the button hide has to be at the top
$("button").click(function () {
$(".holcomb, .lunden, .maggie, .rosewood").hide("slow");
$("button").hide("fast");
});
// examples show hide
$(document).ready(function() {
$("a#holcomb").click(function () {
$(".holcomb, .lunden, .maggie, .rosewood").hide("fast");
$("button").hide("fast")
$(".holcomb").slideDown(1500);
$("button#holcomb").show("fast")
});
});
$(document).ready(function() {
$("a#lunden").click(function () {
$(".holcomb, .lunden, .maggie, .rosewood").hide("fast");
$("button").hide("fast")
$(".lunden").slideDown(1500);
$("button#lunden").show("fast")
});
});
$(document).ready(function() {
$("a#maggie").click(function () {
$(".holcomb, .lunden, .maggie, .rosewood").hide("fast");
$("button").hide("fast")
$(".maggie").slideDown(1500);
$("button#maggie").show("fast")
});
});
$(document).ready(function() {
$("a#rosewood").click(function () {
$(".holcomb, .lunden, .maggie, .rosewood").hide("fast");
$("button").hide("fast")
$(".rosewood").slideDown(1500);
$("button#rosewood").show("fast")
});
});
</script>
我只需要帮助简化这个脚本。
所发生的一切是,我有一些链接,当您单击它们时,会显示一个 div (带有一个类)。然后,链接旁边还会弹出一个按钮,然后当您单击它时,它会关闭(显然),或者当您单击另一个链接时,它会关闭当前打开的 div 并打开另一个 div。
<script>
$(document).ready(function () {
// for some reason the button hide has to be at the top
$("button").click(function () {
$(".holcomb, .lunden, .maggie, .rosewood").hide("slow");
$("button").hide("fast");
});
// examples show hide
$(document).ready(function() {
$("a#holcomb").click(function () {
$(".holcomb, .lunden, .maggie, .rosewood").hide("fast");
$("button").hide("fast")
$(".holcomb").slideDown(1500);
$("button#holcomb").show("fast")
});
});
$(document).ready(function() {
$("a#lunden").click(function () {
$(".holcomb, .lunden, .maggie, .rosewood").hide("fast");
$("button").hide("fast")
$(".lunden").slideDown(1500);
$("button#lunden").show("fast")
});
});
$(document).ready(function() {
$("a#maggie").click(function () {
$(".holcomb, .lunden, .maggie, .rosewood").hide("fast");
$("button").hide("fast")
$(".maggie").slideDown(1500);
$("button#maggie").show("fast")
});
});
$(document).ready(function() {
$("a#rosewood").click(function () {
$(".holcomb, .lunden, .maggie, .rosewood").hide("fast");
$("button").hide("fast")
$(".rosewood").slideDown(1500);
$("button#rosewood").show("fast")
});
});
</script>
I just need help with simplifying this script.
All that happens is, I have some links and when you click them a div (with a class) shows. Then a button also pops up beside the link, and then when you click it closes (obviously) or when you click another link it closes the currently opened div and opens the other div.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更好地应用类将使此代码更简单,但是使用您所拥有的...
Simply better application of classes would make this code simpler, but working with what you have...
向您想要显示/隐藏的所有元素添加一个类,然后您可以使用以下方法完成这一切:
Add a class to all of the elements you want to have this show/hide thing work on then you can do this all with: