php使用DOMDocument更改div但不更新页面
16lz
2021-01-22
I'm not sure what I'm doing wrong but I'm getting the right nodeValue for what I want. It's just not updating when the php script is done. Here's the code:
我不确定我做错了什么但是我得到了正确的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"
more of the HTML that is around it
更多围绕它的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>
I'm not sure what I'm doing wrong. If I do dom->save it rewrites everything (even php code) so I don't think that's a good idea.
我不确定我做错了什么。如果我做dom->保存它会重写所有内容(甚至PHP代码),所以我认为这不是一个好主意。
1 个解决方案
#1
0
I don't understand why you're using DOMDocument
for this. Can't you just do this:
我不明白你为什么要使用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>
更多相关文章
- 几个有用的php字符串过滤,转换函数代码
- PHP的$ _GET和URL重写
- 在Web Page中包含PHP代码
- php mail函数一段好的代码
- 用于上传多个文件的PHP代码
- (phpQuery)对网站产品信息采集代码的优化
- 韩顺平_php从入门到精通_视频教程_学习笔记_源代码图解_PPT文档
- 【MySQL 技巧分享】 mysql -e 加 v 简化代码
- Oracle相当于MySQL代码“插入虚拟”以返回错误消息