简体   繁体   English

PHP:读取自己的HTML页面的DOM信息

[英]PHP: Reading DOM info of own HTML page

So, let's say I have only one file on my server called index.php : 因此,假设我的服务器上只有一个名为index.php的文件:

<!DOCTYPE html>
<html>    
  <body style="background-color: orange;">
    <!-- On/Off button's picture -->
    <?php    
      echo ("<img id='button' src='data/button.jpg' alt='off'/>");
    ?>
   </body>
</html>

Now, let's say I attach a JavaScript that changes the buttons ALT to ON when I click the button at some point. 现在,假设我附加了一个JavaScript,该JavaScript会在我单击按钮时将按钮ALT更改为ON。

How can I read the DOM elements on this page using PHP? 如何使用PHP阅读此页面上的DOM元素? So far, I've found this code: 到目前为止,我已经找到以下代码:

$dom = new DOMDocument;
$dom->loadHTML($html);
$main = $dom->getElementById('alt');

but I can't figure out how I would fit that in my code above, what page should I call loadHTML() for? 但是我不知道如何在上面的代码中适应它,我应该在哪个页面调用loadHTML()?

How do I make PHP read DOM elements it generated using echo? 如何使PHP读取使用echo生成的DOM元素?

First of all, your button has id button , so searching for the id alt would return no results. 首先,您的按钮具有id button ,因此搜索id alt不会返回任何结果。

Second: The piece of php you found reads a page $html (probably file_get_contents('some_url') , puts it in the variable dom and parses it, searching for the id alt . In your case it would read your own page, breaking it down in pieces looking for that button. 第二:您发现file_get_contents('some_url') php读取了一个$ html页面(可能是file_get_contents('some_url') ,将其放入变量dom并进行了解析,搜索了id alt 。零散地寻找那个按钮。

Thats of no use if you can read the status with javascript on your own page. 如果您可以在自己的页面上使用javascript读取状态,那就没用了。 You can then pass it to php trough an ajax-call or by adding it to the url like mypage.php?button=off 然后,您可以通过ajax调用将其传递到php,也可以将其添加到类似mypage.php?button=off的url中

$ main = $ dom-> getElementById('button');

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

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