Javascript 有没有更好的方法来编写这个显示/隐藏JQuery代码?

Javascript 有没有更好的方法来编写这个显示/隐藏JQuery代码?,javascript,jquery,html,css,optimization,Javascript,Jquery,Html,Css,Optimization,好了,我终于让它按我想要的方式工作了 我需要它有不同的按钮显示/隐藏和隐藏,如果另一个DIV被要求显示,我还需要把我的a标签远离我的DIV标签,以及添加和删除活动类。但是,即使它有效,我知道它真的很混乱 这里有一个链接: 以下是JS: $(document).ready(function() { // Feature box 1 $('#show1').click(function() { $('#show2,#show3,#show4,#show5,#sho

好了,我终于让它按我想要的方式工作了

我需要它有不同的按钮显示/隐藏和隐藏,如果另一个DIV被要求显示,我还需要把我的a标签远离我的DIV标签,以及添加和删除活动类。但是,即使它有效,我知道它真的很混乱

这里有一个链接:

以下是JS:

$(document).ready(function() {

    // Feature box 1

    $('#show1').click(function() {
        $('#show2,#show3,#show4,#show5,#show6').removeClass("active");
        $("#feature-2,#feature-3,#feature-4,#feature-5,#feature-6").hide();
        $("#feature-1").show();
        $(this).addClass("active");
    });

    $('#hide1').click(function() {
        $(this).parent().hide();
        $('#show1').removeClass("active");
    });

    // Feature box 2

    $('#show2').click(function() {
        $('#show1,#show3,#show4,#show5,#show6').removeClass("active");
        $("#feature-1,#feature-3,#feature-4,#feature-5,#feature-6").hide();
        $("#feature-2").show();
        $(this).addClass("active");
    });

    $('#hide2').click(function() {
        $(this).parent().hide();
        $('#show2').removeClass("active");
    });

    // Feature box 3

    $('#show3').click(function() {
        $('#show1,#show2,#show4,#show5,#show6').removeClass("active");
        $("#feature-1,#feature-2,#feature-4,#feature-5,#feature-6").hide();
        $("#feature-3").show();
        $(this).addClass("active");
    });

    $('#hide3').click(function() {
        $(this).parent().hide();
        $('#show3').removeClass("active");
    });

    // Feature box 4

    $('#show4').click(function() {
       $('#show1,#show2,#show3,#show5,#show6').removeClass("active");
        $("#feature-1,#feature-2,#feature-3,#feature-5,#feature-6").hide();
        $("#feature-4").show();
        $(this).addClass("active");
    });

    $('#hide4').click(function() {
        $(this).parent().hide();
        $('#show4').removeClass("active");
    });

    // Feature box 5

    $('#show5').click(function() {
       $('#show1,#show2,#show3,#show4,#show6').removeClass("active");
        $("#feature-1,#feature-2,#feature-3,#feature-4,#feature-6").hide();
        $("#feature-5").show();
        $(this).addClass("active");
    });

    $('#hide5').click(function() {
        $(this).parent().hide();
        $('#show5').removeClass("active");
    });

    // Feature box 6

    $('#show6').click(function() {
       $('#show1,#show2,#show3,#show4,#show5').removeClass("active");
        $("#feature-1,#feature-2,#feature-3,#feature-4,#feature-5").hide();
        $("#feature-6").show();
        $(this).addClass("active");
    });

    $('#hide6').click(function() {
        $(this).parent().hide();
        $('#show6').removeClass("active");
        });
    });
以下是HTML:

<a href="#" id="show1">Show</a>
<a href="#" id="show2">Show</a>
<a href="#" id="show3">Show</a>
<a href="#" id="show4">Show</a>
<a href="#" id="show5">Show</a>
<a href="#" id="show6">Show</a>

<div id="feature-1">
    Fill this space with really interesting content 1. <a href="#" id="hide1">Hide</a>
</div>

<div id="feature-2">
    Fill this space with really interesting content 2. <a href="#" id="hide2">Hide</a>
</div>


<div id="feature-3">
    Fill this space with really interesting content 3. <a href="#" id="hide3">Hide</a>
</div>

<div id="feature-4">
    Fill this space with really interesting content 4. <a href="#" id="hide4">Hide</a>
</div>

<div id="feature-5">
    Fill this space with really interesting content 5. <a href="#" id="hide5">Hide</a>
</div>

<div id="feature-6">
    Fill this space with really interesting content 6. <a href="#" id="hide6">Hide</a>
</div>

您可以使用类而不是ID。根据您当前的标记,我已缩小了您的代码,请尝试以下操作:

$(document).ready(function() {

    $('a[id^=show]').click(function() {
        var which = this.id.slice(-1);
        $(this).addClass("active").siblings('a').removeClass("active");
        $("div[id^='feature']").hide();
        $("#feature-"+which).show();
    });

    $('a[id^=hide]').click(function() {
        var which = this.id.slice(-1);
        $(this).parent().hide();
        $('#show'+which).removeClass("active");
    });

});

您可以使用类而不是ID。根据您当前的标记,我已缩小了您的代码,请尝试以下操作:

$(document).ready(function() {

    $('a[id^=show]').click(function() {
        var which = this.id.slice(-1);
        $(this).addClass("active").siblings('a').removeClass("active");
        $("div[id^='feature']").hide();
        $("#feature-"+which).show();
    });

    $('a[id^=hide]').click(function() {
        var which = this.id.slice(-1);
        $(this).parent().hide();
        $('#show'+which).removeClass("active");
    });

});

几乎总是有更好的方法

JavaScript:

HTML:我稍微改变了你的HTML结构

<div id="links">
    <a href="#">Show</a>
    <a href="#">Show</a>
    ...
</div>

<div id="features">
    <div class="feature">
        Fill this space with really interesting content 1. <a href="#" class="hide">Hide</a>
    </div>
    <div class="feature">
        Fill this space with really interesting content 2. <a href="#" class="hide">Hide</a>
    </div>
    ...
</div>

几乎总是有更好的方法

JavaScript:

HTML:我稍微改变了你的HTML结构

<div id="links">
    <a href="#">Show</a>
    <a href="#">Show</a>
    ...
</div>

<div id="features">
    <div class="feature">
        Fill this space with really interesting content 1. <a href="#" class="hide">Hide</a>
    </div>
    <div class="feature">
        Fill this space with really interesting content 2. <a href="#" class="hide">Hide</a>
    </div>
    ...
</div>
我已经最小化了你的一些JS代码,希望对你有所帮助


我已经最小化了你的一些JS代码,希望它能对你有所帮助

?表示有问题的div的数字。你能用什么方法利用这些信息?你能用这样的方法吗,除了什么以外,每个块都是一样的?表示有问题的div的数字。你有没有办法利用这些信息?你能用Works吗!它如何知道哪个标签被分配给哪个DIV?代码使用索引。因此,当您单击divlinks中的第四个链接时,将显示divfeatures中的第四个.feature容器。非常棒!它如何知道哪个标签被分配给哪个DIV?代码使用索引。因此,当您单击divlinks中的第四个链接时,将显示divfeatures中的第四个.feature容器。
<a href="#" onclick="myfunction('show1','feature-1')" class="anchors" id="show1">Show</a>
<a href="#" onclick="myfunction('show2','feature-2')" class="anchors" id="show2">Show</a>
<a href="#" onclick="myfunction('show3','feature-3')" class="anchors" id="show3">Show</a>
<a href="#" onclick="myfunction('show4','feature-4')" class="anchors" id="show4">Show</a>
<a href="#" onclick="myfunction('show5','feature-5')" class="anchors" id="show5">Show</a>
<a href="#" onclick="myfunction('show6','feature-6')" class="anchors" id="show6">Show</a>

<div class="features" id="feature-1">
Fill this space with really interesting content 1. <a href="#" onclick="$(this).parent().hide();">Hide</a>
</div>

<div class="features"  id="feature-2">
Fill this space with really interesting content 2. <a href="#" onclick="$(this).parent().hide();">Hide</a>
</div>


<div class="features" id="feature-3">
Fill this space with really interesting content 3. <a href="#" onclick="$(this).parent().hide();">Hide</a>
</div>

<div class="features" id="feature-4">
Fill this space with really interesting content 4. <a href="#" onclick="$(this).parent().hide();">Hide</a>
</div>

<div class="features" id="feature-5">
Fill this space with really interesting content 5. <a href="#" onclick="$(this).parent().hide();">Hide</a>
</div>

<div class="features" id="feature-6">
Fill this space with really interesting content 6. <a href="#" onclick="$(this).parent().hide();">Hide</a>
</div>
/// javascript

function myfunction(anchor_id, feature_id){
 $('.anchors').removeClass("active");
    $(".features").hide();
    $("#"+feature_id).show();
    $("#"+anchor_id).addClass("active");
}