【问题标题】:Drupal/Jquery: hide parent div if child is emptyDrupal/Jquery:如果孩子为空,则隐藏父 div
【发布时间】:2014-05-19 06:08:51
【问题描述】:

我很难解决这个问题。我希望实现的是,当works-left 和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--> 

【问题讨论】:

  • 您可以在 div id="latest-works-container" 之前使用 isset 检查它们,如果没有一个 isset 则不会渲染整个 div

标签: javascript jquery html drupal drupal-7


【解决方案1】:

您的元素内容来自 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; ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 2022-11-24
    • 2015-11-04
    • 2017-08-24
    • 2021-11-24
    相关资源
    最近更新 更多