1

我不确定我做错了什么,但我得到了我想要的正确 nodeValue 。php脚本完成后它只是不更新​​。这是代码:

    $dom = new DOMDocument();
    //suppress HTML5 and other errors
    libxml_use_internal_errors(true);
    $dom->loadHTMLFile($pageURL);
    libxml_use_internal_errors(false);
    $xpath = new DOMXPath($dom);
    $divContent = $xpath->query("//*[@id='resultStats']/p")->item(0);
    $newText = new DOMText("100 results");
    var_dump($divContent->nodeValue); //returns old test value "400 results" which is correct
    $divContent->removeChild($divContent->firstChild);
    $divContent->appendChild($newText);
    var_dump($divContent->localName); //"p" because i got it from <p> in resultStats
    var_dump($divContent->textContent); //"100 results"
    var_dump($divContent->nodeValue); //"100 results"

更多围绕它的 HTML

    <div class="container">
    <div class="row">
    <div class="resultStats span3 offset1" id="resultStats">
      <p>400 results found.</p>
    </div>
    </div>
    <div class="row">
     <div class="span12">
         <div class="row">
              <div class="span6 offset1">
                  <?php
                         if (isset($_POST['q'])) {
                        //code from above that is executing every time from tests
                         }
                  ?>
               </div>
               <div class="span5">
                  span5
                  </div>
                  </div>
                  </div>
                  </div>

我不确定我做错了什么。如果我这样做 dom->save 它会重写所有内容(甚至是 php 代码),所以我认为这不是一个好主意。

4

1 回答 1

0

我不明白你为什么要使用DOMDocument这个。你不能这样做:

<div class="container">
<div class="row">
<div class="resultStats span3 offset1" id="resultStats">

<?php

  // get new result count somehow in $resultCount

  echo '<p>'.$resultCount.' results found</p>';
?>
</div>
</div>
于 2013-08-04T22:50:47.623 回答