Php 根据页面更改正文的类别

Php 根据页面更改正文的类别,php,javascript,css,Php,Javascript,Css,你好,我有以下问题。我需要根据我现在的页面更改body类。例如,如果我在索引页上,如果我在购物车页上。类名必须是可以使用PHP或JS传递给body的变量。我在JS中非常紧张,所以请给我一个例子,我如何才能做到这一点?或者我可以这样做吗?PHP为ID回显一个变量 <body id="<?php echo $anId; ?>"> PHP为ID返回一个变量 <body id="<?php echo $anId; ?>"> 在PHP中:printf,$

你好,我有以下问题。我需要根据我现在的页面更改body类。例如,如果我在索引页上,如果我在购物车页上。类名必须是可以使用PHP或JS传递给body的变量。我在JS中非常紧张,所以请给我一个例子,我如何才能做到这一点?或者我可以这样做吗?

PHP为ID回显一个变量

<body id="<?php echo $anId; ?>">

PHP为ID返回一个变量

<body id="<?php echo $anId; ?>">
在PHP中:printf,$className 在jQuery中:$'body'.addClassName 在纯JS中:document.getElementsByTagName'body'.className=className

在PHP:printf,$className中 在jQuery中:$'body'.addClassName 在纯JS中:document.getElementsByTagName'body'.className=className

在html中:

<body class="<?php echo $class; ?>">
</body>
在html中:

<body class="<?php echo $class; ?>">
</body>

它不使用js就可以工作。如果您在body标记前面的php代码段中预定义了类名

<?php $class="siteclass"; ?>

<body class="<?php echo $siteclass; ?>">
</body>

它不使用js就可以工作。如果您在body标记前面的php代码段中预定义了类名

<?php $class="siteclass"; ?>

<body class="<?php echo $siteclass; ?>">
</body>

您可以为每个页面创建单独的类,如, 对于index.php=>index类 for cart.php=>cart类

这里有一些代码可以帮助您

CSS

在您的页面中使用如下脚本:

剧本


您可以为每个页面创建单独的类,如, 对于index.php=>index类 for cart.php=>cart类

这里有一些代码可以帮助您

CSS

在您的页面中使用如下脚本:

剧本


你以前试过什么吗?你以前试过什么吗?对不起,可以避免使用JS吗?对不起,可以避免使用JS吗?
<script>
    //Let your url like http://example.com/index.php
    urlArray=window.location.href.split('/'); // will give array
    cls=urlArray[urlArray.length-1].split('.')[0];// will give index
    document.body.className=cls;// add this class to your body element
</script>