简体   繁体   中英

Is there a way to access the HTML element class name a php function is enclosed within?

So for example if I have:

<div class="menu">
    <?php myFunction() ?>
</div>

Is there some way of checking in myFunction() that the wrapping div has a class of menu ?

EDITED

Sorry, misread OP.

You CAN do it, but it would be incredibly difficult (you would use output buffering after PHP has generated the page and then XSL to parse and traverse the outputted document).

A better way would be to use JavaScript since this is client-side, depending on the situation.

If you are calling the PHP function at the point, why not pass the class through to the function:

<div class="menu">
    <?php myFunction("menu") ?>
</div>

Your php function declaration would look like this:

function myFunction($className)
{
    //Your code.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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