简体   繁体   English

使用php获取div的CSS类

[英]Get css class of div with php

I don't even know if this is possible but hopefully someone will be able to point me in the right direction. 我什至不知道这是否可行,但希望有人能够指出正确的方向。

Basically I want to know if there is a way of getting the css class of a div and then displaying content based on that class. 基本上我想知道是否有一种方法来获取div的CSS类,然后根据该类显示内容。

So for example if; 例如:

<body class="home">

Then a div would display as follows; 然后div将显示如下;

<div><p>This is the Home page</p></div>

Like I said, I don't even know if this is possible but any help would be greatly appreciated. 就像我说的,我什至不知道这是否可行,但是任何帮助将不胜感激。 Thanks 谢谢

What you're trying to do can be done with Javascript, but if you want to use php only, then try to use php before you provide the "class" parameter. 您可以尝试使用Javascript完成操作,但是如果您只想使用php,请提供“ class”参数之前尝试使用php。 For example, if $_GET['class']=="home" then <div class="<? echo $_GET['class']?>">some text</div> 例如,如果$_GET['class']=="home"<div class="<? echo $_GET['class']?>">some text</div>

Perhaps, you can use Javascript, with IDs for example: 也许,您可以使用带有ID的Javascript,例如:

<div id="home"></div>

<script>document.getElementById('home').InnerHTML = "this is text for home";</script>

Hope it helps! 希望能帮助到你!

See you point, but you goes the wrong way. 看到你的意思,但是你走错了路。 If you want to put all content in one page differs by some query param, there is no need to do so. 如果要将所有内容放在一个查询参数不同的一页中,则无需这样做。 You just can hide unneeded blocks with css and show them with js. 您可以使用CSS隐藏不需要的块,并使用js显示它们。 On the other hand, if this is some sort of server-side utilization, there is definitely no reason to do so too. 另一方面,如果这是某种服务器端利用率,则绝对没有理由这样做。 On the server you can totally control the output, so make separate templates. 在服务器上,您可以完全控制输出,因此请创建单独的模板。

Is there a reason not to use PHP instead of reading the class of a div? 是否有理由不使用PHP而不是读取div的类?

#index.html
<html>
<?php include /contentDefinitions.php; ?>
...
<?php $content = home; ?>
...
</html>

#contentDefinitions.php
<?php
  if($content = home){
    <p>This is the homepage. I am a happy paragraph.</p>
  }
?>

**this would be a little more efficient with an array or something,
but at the end of the day the easiest thing would just be to include
home.php, page2.php, page3.php etc. as needed instead of going the
route of variables etc... though having an array would let you edit
all the content within one file.

I'm no master of code and have zero familiarity with Joomla, so this may be absolutely useless to you. 我不是代码大师,并且对Joomla零熟悉,所以这对您来说绝对没有用。 :) :)

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

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