简体   繁体   English

如果Div为空,请将其隐藏并更改另一个div的宽度

[英]If Div is empty, hide it and change width of another div

I am new to css and php and am trying to find a way to hide my .item-page aside if the Div is empty. 我是CSS和PHP的新手,如果Div为空,我试图找到一种方法来隐藏我的.item页。 And then change the width of item-page to 100%. 然后将项目页面的宽度更改为100%。 Right now the aside takes up 85px empty or not. 现在,旁边是否占用了85px的空白。 How can do this in my php file? 如何在我的php文件中执行此操作?

Here is my CSS: 这是我的CSS:

item-page { 
position:relative;
  width: 100%;

}

.item-page aside {
  float:left;
  position:absolute;
  width:85px;

}
gk-article {
  font-size:14px;
  line-height:26px !important;
  margin:0 0 80px 110px;

}

Here is my php file: 这是我的php文件:

<div id="main">                                
<jdoc:include type="component" />

</div>

You could set up different css class rules for empty vs not empty. 您可以为空和不为空设置不同的CSS类规则。 These rules would set widths and/or display. 这些规则将设置宽度和/或显示。 Empty element would be zero...or don't even print it. 空元素将为零...甚至不打印它。 Then run whatever test in your server code that determines status...and apply appropriate calsses to the elements 然后在服务器代码中运行确定状态的任何测试...并将适当的校准应用于元素

You've tagged javascript/jquery here, and that's probably what you should use (not PHP). 您在此处标记了javascript / jquery,这可能就是您应该使用的(不是PHP)。

(in jQuery) you can do something like this - use classes as charlietfl suggested: (在jQuery中),您可以执行以下操作-按照charlietfl建议使用类:

if( $('.item-page aside').html() == '') ) { 
    $('.item-page aside').hide();
    $('.item-page').removeClass('isntempty').addClass('isempty');
} else {
    $('.item-page aside').show();
    $('.item-page').removeClass('isempty').addClass('isntempty');
}

CSS: CSS:

.isntempty {
    width: 100%;
}

.isempty {
    width: 50%; /* whatever your default width is */
}

check by js in php: 通过php中的js检查:

<?php
...
echo "<script>if (document.getElementById('divId').innerText=='')
//or document.getElementById('divId').textContent=='' in firefox//
$('#divId').attr('style','width:100%;');
</script>";
...
?>

You can use Javascript, with the jQuery framework you could do this : 您可以使用Javascript,并通过jQuery框架执行以下操作:

<script type="text/javascript">
$( document ).ready(function() {

//Hide if empty
if( $('.item-page').is(':empty') ) {
    $('.item-page').hide();
}

//Change to width 100%
$('.item-page').css({width:'100%'});

});
</script>

Learn more here : 在这里了解更多:

http://api.jquery.com/empty-selector/ http://api.jquery.com/empty-selector/

http://learn.jquery.com/using-jquery-core/document-ready/ http://learn.jquery.com/using-jquery-core/document-ready/

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

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