简体   繁体   English

使用PHP 操作当前页面的DOM?

[英]to manipulate the DOM of the current page using PHP?

I would like to know how to manipulate the DOM of the current page using PHP.我想知道如何使用 PHP 操作当前页面的 DOM。 The explanation is as below:解释如下:

This is the code in my PHP file:这是我的 PHP 文件中的代码:

<div id="edittitle" title="Editing title">
<b>1</b>
</div>
$strhtml=;
    libxml_use_internal_errors( true );
    $dom=new DOMDocument('1.0','utf-8');
    $dom->validateOnParse=false;
    $dom->loadHTML( $strhtml );
    libxml_clear_errors();
    $col=$dom->getElementsByTagName('b');
    echo $col->item(0)->nodeValue;

Actually, what I want is putting the whole html code that exists in the current file (the PHP file) in the variable $strhtml (Please focus on the following line of my code: $strhtml=; ).实际上,我想要的是将当前文件(PHP 文件)中存在的整个 html 代码放在变量$strhtml (请关注我代码的以下行: $strhtml=; )。 Is there any idea about how to do that?有没有关于如何做到这一点的想法?

Thanks in advance.提前致谢。

If you really want to you can do this by putting all of your HTML in an output buffer while / before sending it to the browser (the latter can be easily done with output control ).如果你真的想要,你可以通过在将所有 HTML 发送到浏览器之前将它放在输出缓冲区中来做到这一点(后者可以通过输出控制轻松完成)。 Then you can DOM parse the "captured" HTML.然后您可以 DOM 解析“捕获”的 HTML。

But you should ask yourself what you are really trying to achieve and if this is not better suited for client-side (ie Javascript) code.但是你应该问问自己你真正想要实现什么,以及这是否更适合客户端(即 Javascript)代码。

PHP is a server-side language and is only executed on the server, any DOM manipulations must be handled by client-side Javascript. PHP 是一种服务器端语言,只能在服务器上执行,任何 DOM 操作都必须由客户端 Javascript 处理。

http://callmenick.com/post/basics-javascript-dom-manipulation http://callmenick.com/post/basics-javascript-dom-manipulation

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

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