【问题标题】:div function inside another div function另一个 div 函数中的 div 函数
【发布时间】:2015-02-08 11:25:09
【问题描述】:

是否可以在 php 中让第二个 div 出现在第一个 div 中,而不是让两个分开的 div 出现? 我想做一个 php dom 系统,但我被这个问题困住了。

这是我在页面返回时得到的 HTML:

<div style="width:
                50px;
            height:
                30px;
            background-color:
                yellow;

                color:white
            ">
                blablabla
</div>
<div style="width:
                200px;
            height:
                50px;
            background-color:
                blue;

                color:white
            ">

</div>

这是 PHP 代码:

<? php

function genDiv($width, $height, $bgColor, $add, $content) {
    echo '
        <div style=\'width:
            '.$width.';
        height:
            '.$height.';
        background-color:
            '.$bgColor.';

            '.$add.'
        \'>
            '.$content.'
        </div>
    ';
}

$block = genDiv(
    '50px',
    '30px',
    'yellow',
    'color:white',
    'blablabla'
);
genDiv(
    '200px', 
    '50px',
    'blue',
    'color:white',
    $block
);

?>

【问题讨论】:

    标签: php html function block


    【解决方案1】:

    您需要重写该函数,使其返回 HTML,而不是直接回显它。

    然后你可以回显第二次调用函数的结果。

    【讨论】:

      【解决方案2】:

      你的函数中没有return,你echo st。直接。

      function genDiv($width, $height, $bgColor, $add, $content) {
          return '
              <div style=\'width:
                  '.$width.';
              height:
                  '.$height.';
              background-color:
                  '.$bgColor.';
      
                  '.$add.'
              \'>
                  '.$content.'
              </div>
          ';
      }
      
      $block = genDiv(
          '50px',
          '30px',
          'yellow',
          'color:white',
          'blablabla'
      );
      echo genDiv(
          '200px', 
          '50px',
          'blue',
          'color:white',
          $block
      );
      

      【讨论】:

        【解决方案3】:

        你也可以用双引号代替单引号。

        我使用此代码获得了所需的输出。

        <?php 
        function genDiv($width,$height,$bgcolor,$add,$content)
        {
            return "<div style='width $width; height:$height;
            background-color: $bgcolor; $add'>
            $content
            </div>";
        }
        $block = genDiv('50px','30px','orange','color:white','blablabla');
        $content = genDiv('200px','50px','blue','color:white;padding-top:20px', $block);
        echo $content; //required output. second div in first div
        ?>
        

        我更改了黄色并添加了填充以清晰可见。

        【讨论】:

        • 你的要求达到了吗
        猜你喜欢
        • 1970-01-01
        • 2012-03-22
        • 2020-04-22
        • 1970-01-01
        • 2017-10-13
        • 2012-06-26
        • 2014-01-26
        • 1970-01-01
        • 2022-11-25
        相关资源
        最近更新 更多