简体   繁体   English

用PHP将属性添加到body标签

[英]Adding attribute to body tag with PHP

I'm trying to add the bgcolor attribute to the body tag with PHP. 我正在尝试使用PHP将bgcolor属性添加到body标签。
Using getElement() and setAttribute() won't work in PHP. 在PHP中无法使用getElement()setAttribute()
Does anyone know a solution? 有人知道解决方案吗?
I want to change the background color of the body with PHP, so any other solutions using PHP would be great too. 我想用PHP更改主体的背景颜色,因此使用PHP的任何其他解决方案也都很好。

Edit: For an assignment that's way outdated we HAVE to use PHP to change the backgroundcolor and we need to use bgcolor. 编辑:对于已经过时的作业,我们不得不使用PHP来更改backgroundcolor,我们需要使用bgcolor。 With a POST form with some radiobuttons and a Submit button we need to change the bgcolor. 对于带有一些单选按钮和提交按钮的POST表单,我们需要更改bgcolor。 Up to this I used echo to ADD a tag after there is a tag already. 到目前为止,在已有标签之后,我使用echo来添加标签。 Any ideas on that? 有什么想法吗?

Edit2: I tried to echo the in the bgcolor attribute, but that seems to stop the full page from loading, so that doesn't work unfortunately. Edit2:我试图在bgcolor属性中回显,但是这似乎阻止了整个页面的加载,因此不幸的是,它无法正常工作。

PHP is server side! PHP是服务器端! If you want to do it with php simple echo it like this 如果您想用php简单地做到这一点,请像这样

<?php echo '<body style="background-color:#000">'; ?>

otherwise you will need to do it in javascript with 否则,您将需要在javascript中使用

document.getElementsByTagName('body')[0].style.backgroundColor = '#000';

hope that help 希望有帮助

A simple solution is to inject this somewhere in your html: 一个简单的解决方案是将其注入html中的某个位置:

<style type="text/css">
    body {
        background-color: <?php echo $color; ?> !important;
    }
</style>

Then before this code is run, define $color to hold a color value like #cccccc . 然后,在运行此代码之前,定义$color来保存#cccccc这样的颜色值。

Well, actually you shoudn't use bgcolor anymore. 好吧,实际上您不应该再使用bgcolor了。 Instead, give a css style to the body tag: 而是给body标签指定css样式:

<?php
   $color = "#004422";
?>

<body style="background-color:<?php echo $color; ?>">

   <!-- CONTENT -->

</body>

You can use inline styles (wheter in <head> , wheter in <body> with scoped attribute) or variables. 您可以使用内联样式(在<head> ,在<body>使用具有scoped属性的)或变量。

...
<style>
  body {
    background-color: <?php echo $body_bgcolor; ?>
  }
</style>
...

or 要么

<body style="background-color: <?php echo $body_bgcolor; ?>">
...
</body>

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

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