简体   繁体   English

需要帮助简化代码

[英]Need help simplifying code

Okay, I have a page on a Drupal install that has multiple divs. 好的,我有一个有多个div的Drupal安装页面。 I wrote a .js to test to see if there was any information inside those divs 我写了一个.js进行测试,看看这些div中是否有任何信息

if ($('.accred').length) {
    $('#accred').show();
}
else{
    $('#accred').hide();
}    
if ($('.admin-req').length) {
    $('#admis').show();
}
else{
    $('#admis').hide();
}    
if ($('.career-opp').length) {
    $('#career').show();
}
else{
    $('#career').hide();
}    
if ($('.co-op-diploma').length) {
    $('#co_op').show();
}
else{
    $('#co_op').hide();
}    
if ($('.prog-out').length) {
    $('#outcomes').show();
}
else{
    $('#outcomes').hide();
}    
if ($('.prog-struc').length) {
    $('#struc').show();
}
else{
    $('#struc').hide();
}    
if ($('.testimonials').length) {
    $('#testimon').show();
}
else{
    $('#testimon').hide();
}    
if ($('.transfer').length) {
    $('#transfer').show();
}
else{
    $('#transfer').hide();
}   
if ($('.tuition').length) {
    $('#tuition').show();
}
else{
    $('#tuition').hide();
}   

to hide or show the links that would allow you to click and show more information about each one, because each div except for one is hidden by default: 隐藏或显示允许您单击并显示有关每个div的更多信息的链接,因为默认情况下隐藏除了一个div之外的每个div:

$(function(){
  $('#descrip').click(function(){
     $('.prog-descrip').show();
     $('.accred').hide(); 
     $('.admin-req').hide();
     $('.career-opp').hide(); 
     $('.co-op-diploma').hide(); 
     $('.prog-out').hide(); 
     $('.prog-struc').hide(); 
     $('.testimonials').hide(); 
     $('.transfer').hide(); 
     $('.tuition').hide(); 
  });
  $('#accred').click(function(){
     $('.prog-descrip').hide();
     $('.accred').show(); 
     $('.admin-req').hide();
 $('.career-opp').hide(); 
     $('.co-op-diploma').hide(); 
     $('.prog-out').hide(); 
     $('.prog-struc').hide(); 
     $('.testimonials').hide(); 
     $('.transfer').hide(); 
     $('.tuition').hide(); 
  });
  $('#admis').click(function(){
     $('.prog-descrip').hide();
     $('.accred').hide(); 
     $('.admin-req').show();
     $('.career-opp').hide(); 
     $('.co-op-diploma').hide(); 
     $('.prog-out').hide(); 
     $('.prog-struc').hide(); 
     $('.testimonials').hide(); 
     $('.transfer').hide(); 
     $('.tuition').hide(); 
  });
  $('#career').click(function(){
     $('.prog-descrip').hide();
     $('.accred').hide(); 
     $('.admin-req').hide();
     $('.career-opp').show(); 
     $('.co-op-diploma').hide(); 
     $('.prog-out').hide(); 
     $('.prog-struc').hide(); 
     $('.testimonials').hide(); 
     $('.transfer').hide(); 
     $('.tuition').hide(); 
  });
  $('#co_op').click(function(){
     $('.prog-descrip').hide();
     $('.accred').hide(); 
     $('.admin-req').hide();
     $('.career-opp').hide(); 
     $('.co-op-diploma').show(); 
     $('.prog-out').hide(); 
     $('.prog-struc').hide(); 
     $('.testimonials').hide(); 
     $('.transfer').hide(); 
     $('.tuition').hide(); 
  });
  $('#outcomes').click(function(){
     $('.prog-descrip').hide();
     $('.accred').hide(); 
     $('.admin-req').hide();
     $('.career-opp').hide(); 
     $('.co-op-diploma').hide(); 
     $('.prog-out').show(); 
     $('.prog-struc').hide(); 
     $('.testimonials').hide(); 
     $('.transfer').hide(); 
     $('.tuition').hide(); 
  });
  $('#struc').click(function(){
     $('.prog-descrip').hide();
     $('.accred').hide(); 
     $('.admin-req').hide();
     $('.career-opp').hide(); 
     $('.co-op-diploma').hide(); 
     $('.prog-out').hide(); 
     $('.prog-struc').show(); 
     $('.testimonials').hide(); 
     $('.transfer').hide(); 
     $('.tuition').hide(); 
  });
  $('#testimon').click(function(){
     $('.prog-descrip').hide();
     $('.accred').hide(); 
     $('.admin-req').hide();
     $('.career-opp').hide(); 
     $('.co-op-diploma').hide(); 
     $('.prog-out').hide(); 
     $('.prog-struc').hide(); 
     $('.testimonials').show(); 
     $('.transfer').hide(); 
     $('.tuition').hide(); 
  });
  $('#transfer').click(function(){
     $('.prog-descrip').hide();
     $('.accred').hide(); 
     $('.admin-req').hide();
     $('.career-opp').hide(); 
     $('.co-op-diploma').hide(); 
     $('.prog-out').hide(); 
     $('.prog-struc').hide(); 
     $('.testimonials').hide(); 
     $('.transfer').show(); 
     $('.tuition').hide(); 
  });
  $('#tuition').click(function(){
     $('.prog-descrip').hide();
     $('.accred').hide(); 
     $('.admin-req').hide();
     $('.career-opp').hide(); 
     $('.co-op-diploma').hide(); 
     $('.prog-out').hide(); 
     $('.prog-struc').hide(); 
     $('.testimonials').hide(); 
     $('.transfer').hide(); 
     $('.tuition').show(); 
  });
});

The client, however, does not like this way of doing it. 但是,客户不喜欢这种做法。 He wants to populate based on if there are more divs. 他想根据是否有更多的div来填充。

So, my question is this: How do I populate all of the divs and the code to show/hide divs based on which link they click dynamically using js or jquery? 所以,我的问题是:如何根据使用js或jquery动态点击的链接来填充所有div和代码来显示/隐藏div? I am NOT a js or jquery guy, so pardon my naive question, if it is in fact naive. 我不是一个js或jquery家伙,所以原谅我天真的问题,如果它实际上是天真的。 Thanks! 谢谢!

EDIT 编辑

Please re-read my question those who have replied. 请重新阅读我的问题。 dynamically populating the information. 动态填充信息。 In other words, the client does not want hard-coded divs. 换句话说,客户端不需要硬编码的div。

更好的解决方案,只需提供隐藏自己的类所需的所有类,并使用.toggle()。

Okay, so here is what I ended up doing. 好的,所以这就是我最终做的事情。 I ended up assigning each link the class of linkeddiv, and a rel of whatever the name of the content div was, like this: 我最终为每个链接分配了linkeddiv的类,以及内容div的任何名称的rel,如下所示:

<a href="#tuition" class="linkeddiv" rel="tuition">Tuition and Fees</a> 
<div class="prog-descrip prog-detail" style="display: none; ">

After that, I set an interval function to test the url of the page, and specifically grab the characters after the has mark. 之后,我设置了一个区间函数来测试页面的url,特别是在有标记后抓取字符。 What this did was test to see if the page had changed, in case the end-user pressed the back button. 这样做是测试页面是否已经改变,以防最终用户按下后退按钮。 If it did, it would automatically update the page to the proper div showing 如果是这样,它会自动将页面更新为正确的div显示

setInterval(function() {
   // parse the url
   current_url = document.location.href;
   after_hash = current_url.split('#');

   if (after_hash[1] == null) {
       if ($('.prog-descrip').length) {
        $('.prog-detail').hide();
        $('.prog-descrip').show();
        $('#prog_link_block').show();
        $('#Program_Areas-1').hide();
       }
   } else {
       $('.prog-detail').hide();
       $('#Program_Areas-1').hide();
       $('.' + after_hash[1]).show();
   }
}, 100);

Once that was done, I just tested which link they clicked, and based on that, I hid or showed the div based on the rel link: 完成后,我只测试了他们点击了哪个链接,并根据它,我根据rel链接隐藏或显示了div:

$('.linkeddiv').each(function() {
    contentdiv = $(this).attr('rel');
    if ($('.' + contentdiv).length == 0) {
        $(this).hide();
    }
});
$('.linkeddiv').click(function() {
    $('.prog-detail').hide();
    contentdiv = $(this).attr('rel');
    $('.' + contentdiv).show();
}); 

This achieved exactly what I was looking for. 这实现了我正在寻找的东西。 Also, it added the functionality of using the back button, instead of leading to a dead link (#). 此外,它添加了使用后退按钮的功能,而不是导致死链接(#)。

You could also do some fun fade or slide if you wanted with the jquery. 如果你想使用jquery,你也可以做一些有趣的淡入淡出或幻灯片。

Hope this helps someone. 希望这有助于某人。

The second section can be shortened to: 第二部分可以缩短为:

$('.accred, .admin-req, .career-opp, .co-op-diploma, .prog-out, .prog-struc,
 .testimonials, .transfer, .tuition').hide(); 
//Show the one you want

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

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