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.

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

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路