douyoufan7881 2014-11-22 20:59
浏览 69
已采纳

在PHP中将变量应用于DIV类

I am attempting to make the TOP and LEFT parameters of this DIV class variable. Here are snippets of my code

First in the style.php file:-

<?php
header("Content-type:text/css");
$top='100';
$left='200';
?>
.testbox {
     float:left;
     position:absolute;
     top:<?php echo $top; ?>px;
     left:<?php echo $left; ?>px;
     background-size:2000px 2000px;
     background-repeat:no-repeat;
     background-color:#FFFFFF;
     width:50px;
     height:100px;
     border:1px solid #b3bdc2;
     z-index:1;

     }

In my main script (gettrains.php)

        $top='100';
        $left='500';
        echo "<div style='top:$top;left:$left' class='testbox'>
        <p>$answer</p>
        </div>";

I have also put the below line into my gettrains.php script

<link href="style.php" type="text/css" rel="stylesheet" />

I basically have two problems:

1) The box is positioned at 100,200 as per style.php and NOT 100,500 .........my new variables

2) Since including the "link href="style.php.............etc at the top of the gettrains.php file it keeps reloading when previously it did not (Its part of an AJAX call (GET) that worked fine before including the LINK to style.php

Any help appreciated. Thanks

  • 写回答

1条回答 默认 最新

  • dqb78642 2014-11-22 21:14
    关注

    This is an odd thing to do, as PHP can only generate the initial HTML source that the browser receives. You're basically saying "I need the box at 100/200 and 100/500". In a battle between CSS rules, the most specific rule wins. Normally that'd be style="..." but in this case you didn't add the px unit to the inline style so the browser rejects it as meaningless.

    The "quick fix" here is to simply fix that problem: echo "<div style='top:${top}px;left:${left}px' class='testbox'> but the real problem here is that you're specifying two positions mutually exclusive positions at the same time without a clear indication of why you're doing the override. Which position does it need? PHP runs server-side so it can't really have any idea why 100/500 is better than 100/200 (what are those numbers based on? Unlike on-page JavaScript, PHP can't check what is good positioning, so these are just magic numbers).

    As simple positioning information for a specific element, it's much better to generate those positions as their own, named, CSS class such as:

    .initial {
      top:  <?php echo $top_override; ?>px;
      left: <?php echo $left_override; ?>px;
    }
    

    and then in the HTML that you generate, use <div class="initial">... so that the master CSS applies, but the .initial class wins, setting the correct left value (since the top values are the same). Then, once the page is rendered by the browser, you'll have to use JavaScript to do any further dynamic positional changes.

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?