0
<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。

4

2 回答 2

1

简单地更好地应用类将使这段代码更简单,但是使用你所拥有的......

$(document).ready(function(){   
    $("button").click(function() {
        $(".holcomb, .lunden, .maggie, .rosewood").hide("slow");
        $("button").hide("fast");
    });

    $("a#holcomb, a#lunden, a#maggie, a#rosewood").click(function () {
       $(".holcomb, .lunden, .maggie, .rosewood").hide("fast");
       $("button").hide("fast");
       $("."+this.id).slideDown(1500);
       $("button#"+this.id).show("fast")
   });
});
于 2011-04-23T01:42:38.757 回答
0

将一个类添加到您希望此显示/隐藏操作的所有元素,然后您可以通过以下方式完成所有操作:

var $allElements = $(".showHide");


$allElements.click(function () {
    $allElements.hide("fast");
    $(this).slideDown(1500);
    /* you'd have to add some logic here for the matching button...
       ...perhaps give it an ID matching the link with a suffix of '-button' 
       or something */
});
于 2011-04-23T01:51:42.230 回答