简体   繁体   English

你怎么能用php隐藏html元素?

[英]How can you hide html element with php?

if you do in php:如果你在 php 中做:

$dom = new DOMDocument();
$dom->loadHTML($pageUrl);
$dom->getElementById(eleId);

How can you hide the element.你怎么能隐藏元素。 I know how you can do it with css and js.我知道如何使用 css 和 js 来实现。 But I want to know it with php.但我想用php知道它。

I'm going to assume that you're trying to have the element initially hidden ie in existence, but not visible, upon the browser's initial rendering and you already know that PHP can't directly instruct the browser to hide elements.我将假设您试图在浏览器初始呈现时最初隐藏元素,即存在但不可见,并且您已经知道 PHP 无法直接指示浏览器隐藏元素。

Following your example, you could do:按照您的示例,您可以执行以下操作:

$eleId = “div_to_hide”;
$dom = new DOMDocument();
$dom->loadHTML($pageUrl);
$target_elem = $dom->getElementById($eleId);
if ($target_elem) {
    $target_elem-> setAttribute('style','display: none;');
}

Or you could create a “script” element containing JavaScript code to execute after the resource has totally downloaded to hide it at run-time.或者,您可以创建一个包含 JavaScript 代码的“脚本”元素,以便在资源完全下载后执行以在运行时隐藏它。

Or you could create a “style” element in your DOMDocument object in which you'd put CSS content referencing the target element (by selector) to apply initial “display: none;”或者,您可以在 DOMDocument 对象中创建一个“style”元素,您可以在其中放置引用目标元素的 CSS 内容(通过选择器)以应用初始“display: none;” CSS. CSS。

Just other ideas, but the snippet above follows your paradigm.只是其他想法,但上面的片段遵循您的范例。

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

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