doudi1449 2015-12-14 20:13
浏览 36
已采纳

在Php文件的If语句中使用DIV的宽度

I have a .php file in which I have a layout that I would like to change depending of a div's width. That div is a parent of the following code and is situated in a other file.

<?php if ($(".container").width() > 400) { ?>

    <div class="sub-container">
        <div">sidepanel-left</div>
        <div>content</div>
        <div>sidepanel-right</div>
    </div>

<?php } else { ?>

    <div class="sub-container">
        <div>sidepanel-left
                <div>sidepanel-right</div>
        </div>
        <div>content</div>
    </div>

<?php } ?>

That gives me a blank page with only Html, head and body tags.

I'm sure it's a pretty basic code that I need, but I'm just starting in PHP/JS and I'm not sure of what I'm doing -_-

Let me know if you need more information!

Thanks in advance for the help! :)

B.

  • 写回答

1条回答 默认 最新

  • donglian4464 2015-12-14 20:33
    关注

    You can not do this in PHP. What you can do is the following:

    From PHP side, output this HTML code:

        <div class="container">
            ...
            <div class="sub-container">
                <div>sidepanel-left</div>
                <div>content</div>
                <div>sidepanel-right</div>
            </div>
    
        </div>
    

    Then in your HTML page, make sure jquery is loaded, and put this:

        <script type="text/javascript">
        $(document).ready(function() { 
    
            $('.container').each(function(){
                var $this = $(this);
                if ( $this.width() > 400 ) {
                    $this.addClass('wide-container');
                }
            });
        }); 
        </script>
    

    Then, in your css file you can setup a class for regular container and wide container, and you can also control the styles of subcontainers:

        .container {
            // default styles here
        }
    
        .container.wide-container {
            // override wide container styles
        }
    
        .container.wide-container > .subcontainer {
            // override subcontainer styles
        }
    

    You might use @media queries to achieve similar result, but @media queries can be applied according to the width of the screen, not a particular container.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 扫描枪扫条形码出现问题
  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?