jQuery 单击 div 的动态 hide/show 内容

jQuery Dynamic hide/show content of div on click

我有 4 个 div 内容不同。每个 div 都应根据您单击的 link 更改其内容。

如果我有 10 divs,我将不得不写 10 次:

$( "#banner2" ).hide()
$( "#banner3" ).hide();
$( "#banner4" ).hide();

    $( "#link2" ).click(function() {
    $( "#banner1, #banner3, #banner4" ).hide()
    $( "#banner2" ).fadeIn( "slow", function() {
    });
});

$( "#link1" ).click(function() {
    $( "#banner2, #banner3, #banner4" ).hide();
    $( "#banner1" ).fadeIn( "slow", function() {
    });
});

$( "#link3" ).click(function() {
    $( "#banner1, #banner2, #banner4" ).hide();
    $( "#banner3" ).fadeIn( "slow", function() {
    });
});

$( "#link4" ).click(function() {
    $( "#banner1, #banner2, #banner3" ).hide();
    $( "#banner4" ).fadeIn( "slow", function() {
    });
});

知道如何让它更有活力吗?

您可以使用 attribute selector 来...

其中 ^= 表示“开头为”。

注意在JS ansd中使用是可以的CSS.

$("[id^='link']").click(function(){
  $("[id^='banner']").hide()
  let num = this.id.replace("link","")
  $("#banner"+num).fadeIn("slow")
})
[id^='link']{
  text-decoration: none;
  color: #dd4ed3;
  font-size: 1.2em;
}
[id^='link']:after{
  content: " | "
}
[id^='banner']{
display: none;
  padding: 2em;
  margin: 2em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<a href="#" id="link1">Banner #1</a>
<a href="#" id="link2">Banner #2</a>
<a href="#" id="link3">Banner #3</a>
<a href="#" id="link4">Banner #4</a>
<a href="#" id="link5">Banner #5</a>
<a href="#" id="link6">Banner #6</a>
<a href="#" id="link7">Banner #7</a>
<a href="#" id="link8">Banner #8</a>
<a href="#" id="link9">Banner #9</a>
<a href="#" id="link10">Banner #10</a>
</div>

<div id="banner1">Banner #1</div>
<div id="banner2">Banner #2</div>
<div id="banner3">Banner #3</div>
<div id="banner4">Banner #4</div>
<div id="banner5">Banner #5</div>
<div id="banner6">Banner #6</div>
<div id="banner7">Banner #7</div>
<div id="banner8">Banner #8</div>
<div id="banner9">Banner #9</div>
<div id="banner10">Banner #10</div>

在所有链接上使用通用 class,就像 Taplar 建议的那样:

$(".bannerLink").click(function(){
  $("[id^='banner']").hide()
  let num = this.getAttribute("data-target")
  $("#banner"+num).fadeIn("slow")
})
.bannerLink{
  text-decoration: none;
  color: #dd4ed3;
  font-size: 1.2em;
}
.bannerLink:after{
  content: " | "
}
[id^='banner']{
display: none;
  padding: 2em;
  margin: 2em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<a href="#" class="bannerLink" data-target="1">Banner #1</a>
<a href="#" class="bannerLink" data-target="2">Banner #2</a>
<a href="#" class="bannerLink" data-target="3">Banner #3</a>
<a href="#" class="bannerLink" data-target="4">Banner #4</a>
<a href="#" class="bannerLink" data-target="5">Banner #5</a>
<a href="#" class="bannerLink" data-target="6">Banner #6</a>
<a href="#" class="bannerLink" data-target="7">Banner #7</a>
<a href="#" class="bannerLink" data-target="8">Banner #8</a>
<a href="#" class="bannerLink" data-target="9">Banner #9</a>
<a href="#" class="bannerLink" data-target="10">Banner #10</a>
</div>

<div id="banner1">Banner #1</div>
<div id="banner2">Banner #2</div>
<div id="banner3">Banner #3</div>
<div id="banner4">Banner #4</div>
<div id="banner5">Banner #5</div>
<div id="banner6">Banner #6</div>
<div id="banner7">Banner #7</div>
<div id="banner8">Banner #8</div>
<div id="banner9">Banner #9</div>
<div id="banner10">Banner #10</div>

或使用从 .index()...

检索的子索引获得更多灵活性

$(".bannerLink").click(function(){
  $(".banner").hide()
  let num = $(this).index()
  $(".banner").eq(num).fadeIn("slow")
})
.bannerLink{
  text-decoration: none;
  color: #dd4ed3;
  font-size: 1.2em;
}
.bannerLink:after{
  content: " | "
}
.banner{
display: none;
  padding: 2em;
  margin: 2em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<a href="#" class="bannerLink">Banner #1</a>
<a href="#" class="bannerLink">Banner #2</a>
<a href="#" class="bannerLink">Banner #3</a>
<a href="#" class="bannerLink">Banner #4</a>
<a href="#" class="bannerLink">Banner #5</a>
<a href="#" class="bannerLink">Banner #6</a>
<a href="#" class="bannerLink">Banner #7</a>
<a href="#" class="bannerLink">Banner #8</a>
<a href="#" class="bannerLink">Banner #9</a>
<a href="#" class="bannerLink">Banner #10</a>
</div>

<div class="banner">Banner #1</div>
<div class="banner">Banner #2</div>
<div class="banner">Banner #3</div>
<div class="banner">Banner #4</div>
<div class="banner">Banner #5</div>
<div class="banner">Banner #6</div>
<div class="banner">Banner #7</div>
<div class="banner">Banner #8</div>
<div class="banner">Banner #9</div>
<div class="banner">Banner #10</div>

简而言之...您可以通过多种方式...;)