简体   繁体   English

如何使用 PHP 清除页面中的所有元素?

[英]How to clear all elements in a page with PHP?

Is there any way to clear all html elements in a php page?有没有办法清除php页面中的所有html元素?

For example I have 100 html elements in my page, is there anyway to remove them?例如,我的页面中有 100 个 html 元素,是否可以删除它们?

As we know with javascript we have innerHTML but in PHP what?正如我们所知道的 javascript 我们有innerHTML但在 PHP 中呢?

clear all html elements in a php page清除php页面中的所有html元素

That doesn't make sense.那没有意义。 HTML elements only exist in the DOM after PHP has executed and sent an HTML document to the browser. HTML 元素仅在 PHP 执行并向浏览器发送 HTML 文档后才存在于 DOM 中。 Server-side, where PHP executes, there are no elements to remove.服务器端,PHP 执行的地方,没有要删除的元素。

If you're trying to manipulate the HTML you've already output, you need to capture it with output buffering (see ob_start , ob_get_contents and ob_end_clean ) but if your goal is to "clear all html elements", presumably so you can output a different set of elements, you simply need to not output anything in the first case.如果您尝试操作已经输出的 HTML,则需要使用输出缓冲来捕获它(请参阅ob_startob_get_contentsob_end_clean )但如果​​您的目标是“清除所有 html 元素”,大概是这样您就可以输出不同的元素集,您只需要在第一种情况下不输出任何内容 If this sounds like what you're trying to accomplish, you need to look into simple conditional statements like if / else .如果这听起来像您要完成的任务,那么您需要查看简单的条件语句,例如if / else

as we know with javascript we have innerHTML but in php what ?正如我们所知道的 javascript 我们有 innerHTML 但在 php 中是什么?

There is no PHP-equivalent because PHP doesn't have access to the client-side DOM.没有 PHP 等效项,因为 PHP 无法访问客户端 DOM。 It is purely a server-side technology, and the output of your PHP script is the input to the browser.它纯粹是一种服务器端技术,您的 PHP 脚本的输出是浏览器的输入。 The DOM and its elements are generated long after your PHP script has executed. DOM 及其元素是在 PHP 脚本执行很久之后生成的。 If you have an XHTML fragment in a string, and you want to parse/manipulate it, you can use xpath .如果字符串中有 XHTML 片段,并且想要解析/操作它,则可以使用xpath

如果您的问题是“清除 php 文件中的 html 元素”,那么答案是: strip_tags()

$string = '<p>hello</p>';

echo strip_tags($string);

Try this:试试这个:

<?php
if(//why you want to clear the elements){ 
echo "<script language=\"javascript\">";
?>

//Append all elements in <div id="body">
var body = document.getElementById("body");
body.innerHTML ="";

<?php
echo "</script>";

#Output your new element
echo "New elements.";
}
?>

Try this, it should definitely work.试试这个,它肯定可以工作。

<?php
echo "<script>document.write('');</script>";
?>

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

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