简体   繁体   English

jquery显示/隐藏div中没有​​硬编码的内容

[英]jquery show/hide content in div without hardcoding

I have 3 divs, there is content(images) inside of them. 我有3个div,里面有内容(图像)。 When you click on one of them content of it becomes visible and content of other two becomes hidden. 当您单击其中一个时,其内容将变为可见,其他两个内容将变为隐藏状态。 I've been managed to achieve that with jquery code(you can check for it below), but the bad thing it's hardcoded, is there any other methods do do it? 我已经设法用jquery代码来实现它(你可以在下面检查它),但是它是硬编码的坏事,有没有其他方法可以做到这一点?

PS: " https://solomia-gera.github.io/ " is the site itself if you want to have a look. PS:“ https://solomia-gera.github.io/ ”是网站本身,如果你想看看。

---------------------------------------------------------THE CODE----------------------------------------------------- -------------------------------------------------- - - - -编码 - - - - - - - - - - - - - - - - - - - - - ------------

<!-- Hide/Show DIV1 content-->
<script>
// Hide content in div with id#mox when cliked on div with id#content
  $("#mox2").on("click", function () {
  $("#content1").hide();
  });
  $("#mox3").on("click", function () {
  $("#content1").hide();
  });
// Show content in div with id#mox when cliked on div with id#content
  $("#mox1").on("click", function () {
  $("#content1").show();
  });
</script>

<!-- Hide/Show DIV2 content-->
<script>
  $("#content2").hide();
// Hide content in div with id#mox when cliked on div with id#content
  $("#mox1").on("click", function () {
  $("#content2").hide();
  });
  $("#mox3").on("click", function () {
  $("#content2").hide();
  });
// Show content in div with id#mox when cliked on div with id#content
  $("#mox2").on("click", function () {
  $("#content2").show();
  });
</script>

<!-- Hide/Show DIV3 content-->
<script>
$("#content3").hide();
// Hide content in div with id#mox when cliked on div with id#content
 $("#mox2").on("click", function () {
 $("#content3").hide();
  });
 $("#mox1").on("click", function () {
 $("#content3").hide();
 });
  // Show content in div with id#mox when cliked on div with id#content
 $("#mox3").on("click", function () {
 $("#content3").show();
 });

Given that all three have a common class mox , and if you add a common class content to each of the content divs you can do this: 鉴于所有三个都有一个共同的类mox ,并且如果你为每个内容div添加一个公共类content ,你可以这样做:

$(".mox").click(function() {
    $(this).children(".content").show();
    $(".mox").not(this).children(".content").hide();
}

Explanation: the function is called whenever you click on any element with a mox class. 说明:只要单击具有mox类的任何元素,就会调用该函数。 this selector lets you select the particular instance of the class that was clicked, children() selects any child elements and when we put a selector as an argument for children() , it selects all child elements that match that selector. this选择器允许你选择被点击的类的特定实例, children()选择任何子元素,当我们将选择器作为children()的参数时,它选择与该选择器匹配的所有子元素。 Second line works similarly, with an addition of not(this) , so the second line reads hide all elements that match content class and are children of any element with class mox , but this one. 第二行的工作方式类似,添加了not(this) ,因此第二行读取隐藏与content类匹配的所有元素,并且是具有类mox的任何元素的子元素,但是这一行。

EDIT: If for some reason you do not want to assign a common class to all content divs, the following function will work as is: 编辑:如果由于某种原因你不想为所有内容div分配一个公共类,下面的函数将按原样运行:

$(".mox").click(function() {
    $(this).children("[id^=content]").show();
    $(".mox").not(this).children("[id^=content]").hide();
}

Here I used attributeStartsWith selector, read more here . 这里我使用了attributeStartsWith选择器, 在这里阅读更多

There is much better way for doing this You can simply use the following code 有更好的方法这样做你可以简单地使用以下代码

$('.content').on('click', '.img-heading', function(){
    $(this).parent('.content').siblings('.content').removeClass('active');
    $(this).parent('.content').addClass('active');
})

Codepen link Codepen链接

if your content is outside mox , you can apply a same class for each element and add a data for show only what you want. 如果您的内容位于mox之外,则可以为每个元素应用相同的类,并添加数据以仅显示您想要的内容。

 $(".mox").on("click", function () { var content = $(".content") content.hide(); content.eq($(this).attr("data-content")-1).show(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="mox" data-content="1">Show one</div> <div class="mox" data-content="2">Show two</div> <div class="mox" data-content="3">Show three</div> <div class="content" style="display:none;width:100px;height:100px; background:red" ></div> <div class="content" style="display:none;width:100px;height:100px; background:blue" ></div> <div class="content" style="display:none;width:100px;height:100px; background:green" ></div> 

try this code 试试这段代码

  $('.mox').on('click', function(){ $('.image_list').hide(); var $index = $('.mox').index(this); $('.image_list').eq($index).show(); }); 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="box"> <div id="mox1" class="mox"> <p class="hide"> O<br>V<br>E<br>R<br>V<br>I<br>E<br>W </p> <div id="content1" class="image_list"> <img src="https://solomia-gera.github.io/img/1.jpg"> <img src="https://solomia-gera.github.io/img/2.jpg"> <img src="https://solomia-gera.github.io/img/3.jpg"> <img src="https://solomia-gera.github.io/img/4.jpg"> <img src="https://solomia-gera.github.io/img/5.jpg"> <img src="https://solomia-gera.github.io/img/6.jpg"> <img src="https://solomia-gera.github.io/img/7.jpg"> <img src="https://solomia-gera.github.io/img/8.jpg"> </div> </div> <!-- ALBUMS --> <div id="mox2" class="mox"> <p class=""> A<br>L<br>B<br>U<br>M<br>S </p> <div id="content2" class="image_list"> <img src="https://solomia-gera.github.io/img/1.jpg"> <img src="https://solomia-gera.github.io/img/2.jpg"> <img src="https://solomia-gera.github.io/img/3.jpg"> <img src="https://solomia-gera.github.io/img/4.jpg"> <img src="https://solomia-gera.github.io/img/5.jpg"> <img src="https://solomia-gera.github.io/img/6.jpg"> <img src="https://solomia-gera.github.io/img/7.jpg"> <img src="https://solomia-gera.github.io/img/8.jpg"> </div> </div> <!-- ABOUT --> <div id="mox3" class="mox"> <p class=""> A<br>B<br>O<br>U<br>T </p> <div id="content3" class="image_list"> <img src="https://solomia-gera.github.io/img/1.jpg"> <img src="https://solomia-gera.github.io/img/2.jpg"> <img src="https://solomia-gera.github.io/img/3.jpg"> <img src="https://solomia-gera.github.io/img/4.jpg"> <img src="https://solomia-gera.github.io/img/5.jpg"> <img src="https://solomia-gera.github.io/img/6.jpg"> <img src="https://solomia-gera.github.io/img/7.jpg"> <img src="https://solomia-gera.github.io/img/8.jpg"> </div> </div> </div> 

Based on your code: 根据您的代码:

 function myCustomHide(contentIds, moxsIds){ contentIds.forEach(function(element, index) { index !== 0 ? $("#"+element).hide(): null; moxsIds.forEach(function(moxElem, moxIndex){ if(index !== moxIndex){ $("#"+moxElem).on("click", function(){ $("#"+element).hide(); }); } }); $("#"+moxsIds[index]).on("click", function(){ $("#"+element).show(); }); }); } var contentsIds = [ "content1", "content2", "content3"]; var moxsIds = [ "mox1", "mox2", "mox3"]; myCustomHide(contentsIds, moxsIds); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="box"> <div id="mox1"> Overview </div> <div id="content1" class="image_list"> <img src="https://solomia-gera.github.io/img/1.jpg"> <img src="https://solomia-gera.github.io/img/2.jpg"> <img src="https://solomia-gera.github.io/img/3.jpg"> <img src="https://solomia-gera.github.io/img/4.jpg"> <img src="https://solomia-gera.github.io/img/5.jpg"> <img src="https://solomia-gera.github.io/img/6.jpg"> <img src="https://solomia-gera.github.io/img/7.jpg"> <img src="https://solomia-gera.github.io/img/8.jpg"> </div> <!-- ALBUMS --> <div id="mox2"> Albums </div> <div id="content2" class="image_list"> <img src="https://solomia-gera.github.io/img/1.jpg"> <img src="https://solomia-gera.github.io/img/2.jpg"> <img src="https://solomia-gera.github.io/img/3.jpg"> <img src="https://solomia-gera.github.io/img/4.jpg"> <img src="https://solomia-gera.github.io/img/5.jpg"> <img src="https://solomia-gera.github.io/img/6.jpg"> <img src="https://solomia-gera.github.io/img/7.jpg"> <img src="https://solomia-gera.github.io/img/8.jpg"> </div> <!-- ABOUT --> <div id="mox3"> About </div> <div id="content3" class="image_list"> <img src="https://solomia-gera.github.io/img/1.jpg"> <img src="https://solomia-gera.github.io/img/2.jpg"> <img src="https://solomia-gera.github.io/img/3.jpg"> <img src="https://solomia-gera.github.io/img/4.jpg"> <img src="https://solomia-gera.github.io/img/5.jpg"> <img src="https://solomia-gera.github.io/img/6.jpg"> <img src="https://solomia-gera.github.io/img/7.jpg"> <img src="https://solomia-gera.github.io/img/8.jpg"> </div> </div> 

or check the fiddle for slight different html: https://jsfiddle.net/oy7vj7fq/2/ 或检查小提琴的不同html: https//jsfiddle.net/oy7vj7fq/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM