Drupal / Jquery:如果child为空,则隐藏父div

时间:2014-05-19 06:08:51

标签: javascript jquery html drupal drupal-7

我很难解决这个问题。我希望实现的是当left-works和works-right为空时(记住它使用drupal预处理代码的函数),父div(latest-works-container)将消失。

我想知道是否有一些解决问题的方法。我遇到的是编辑bartik中的template.php代码或其他一些可能的解决方案?

function hybrid_preprocess_html(&$variables) {
  if (!empty($variables['page']['featured'])) {
    $variables['classes_array'][] = 'featured';
  }

  if (!empty($variables['page']['services_first'])
    || !empty($variables['page']['services_second'])
    || !empty($variables['page']['services_third'])
    || !empty($variables['page']['services_fourth'])) {
    $variables['classes_array'][] = 'services';
  }  


    <div id="latest-works-container"><!--latest-works-container-->
      <div id="latest-works"><!--latest-works-->
        <div class="works-left">
            <?php print render($page['portfolio_works_first']); ?>
       </div>  
       <div class="works-right">
            <?php print render($page['portfolio_works_second']); ?>
       </div>  
       <div class="works-left">
            <?php print render($page['portfolio_works_third']); ?>
       </div> 
       <div class="works-right">
            <?php print render($page['portfolio_works_fourth']); ?>
       </div> 
      </div><!--/latest-works-->
    </div><!--/latest-works-container--> 

1 个答案:

答案 0 :(得分:0)

您的元素内容来自PHP,因此您无需使用jQuery删除div。

相反,您应该编辑视图文件。例如,你有render()方法,它给出了div的内容。

$contentBlockFirstLeft = render($page['portfolio_works_first']);
$contentBlockSecondLeft = render($page['portfolio_works_third']);
$contentBlockFirstRight = render($page['portfolio_works_second']);
$contentBlockSecondRight = render($page['portfolio_works_fourth']);

$isLeftBlockEmpty = empty($contentBlockFirstLeft) && empty($contentBlockSecondLeft);
$isRightBlockEmpty = empty($contentBlockFirstRight) && empty($contentBlockSecondRight);

$showAllBlocks = true;
if ($isLeftBlockEmpty && $isRightBlockEmpty) {
    $showAllBlocks = false;
}

因此,您已经知道输出元素所需的所有信息,例如,您将拥有下一个视图。请随意根据所需的业务逻辑更改变量名称,这些只是虚拟名称。

<?php if ($showAllBlocks): ?>
  <div id="latest-works-container"><!--latest-works-container-->
    <div id="latest-works"><!--latest-works-->
      <div class="works-left">
        <?php echo $contentBlockFirstLeft; ?>
      </div>  
      <div class="works-right">
        <?php echo $contentBlockFirstRight; ?>
      </div>  
      <div class="works-left">
        <?php echo $contentBlockSecondLeft; ?>
      </div> 
      <div class="works-right">
        <?php echo $contentBlockSecondRight; ?>
      </div> 
    </div><!--/latest-works-->
</div><!--/latest-works-container--> 
<?php endif; ?>