javascript - 如何从
php中的html标签问题描述
我正在寻找如何<p id="test"></p>
在 PHP 中获得数字。我有选择选项,并且使用下面的代码,我将我的变体 id 选为 html 段落 <p id="test"></p>
,但我需要它作为 PHP 中的数字来运行另一个 PHP 代码来完成某些东西如何<p id="test"></p>
在不使用另一个 java 脚本的情况下转换为 php 中的数字我只需要数字形式我下面的代码作为 php 中的变量号。
我在 WordPress 中使用 woocommerce 插件,我需要基于用户在单个产品页面中选择的选项来显示图像的新方法,而不是切换我需要用变体图像覆盖产品图像,因为我有用户选择的选项和下面的代码但我需要它在 php 中工作我不知道如何<p id="test"></p>
将此代码转换为 php 以获取数字并做我知道如何做的其他事情我只需要<p id="test"></p>
在 php 中获取数字。
<?php
if ( $product->is_type('variable') ) {
?>
<script>
jQuery(document).ready(function($) {
$('input.variation_id').change( function(){
if( '' != $('input.variation_id').val() ) {
var var_id = $('input.variation_id').val();
document.getElementById('test').textContent = var_id;
}
});
});
</script>
<?php
}
?>
我需要<p id="test"></p>
在 php 中获取而不是这个数字。
解决方案
这个问题已经回答了
这里https://stackoverflow.com/a/3577662/10972786
&
这里https://stackoverflow.com/a/11240083/10972786
首先,确保服务器允许您获取数据。
其次,使用 html 解析器来解析数据。
// Gets the webpage
$html = @file_get_contents('https://yourwebsite.com/yourpage');
//if no webpage found die and return an error
if (!$html) {
die('can not get the content!');
}
//Create a new DOM Doc
$doc = new DOMDocument();
//Load the HTML of the page you fetch in the DOM Doc
$doc->loadHTML($html);
//Get the element with the ID test
$content = $doc->getElementById('test');
请注意,如果元素为空,则 Dom 返回 NULL
不要为此使用javascript。
您可以在 PHP 文档中了解有关 DOMDocument 的更多信息:
https://www.php.net/manual/en/class.domdocument.php
这里有更多关于 getElementById
https://www.php.net/manual/en/domdocument.getelementbyid.php
推荐阅读
- maven - Maven Central 上的聚合 Javadoc
- php - 安装时出现“Drupal-8 已安装”错误
- jquery - 将默认值设置为在页面加载时影响 html 表的过滤器
- mysql - 在 Docker 容器中初始化 MySQL 数据
- javascript - 如何解决 JavaScript 中 updateServing 函数不工作的问题?
- apache-spark - 在错误/中断时通过 unix shell 杀死在纱线集群模式下运行的火花流作业
- php - 在 laravel 中将请求转换为 Json
- python - 我无法导入aiohttp,但是好像已经安装了
- python - Pygame - 检测矩形的哪一侧与另一侧的哪一侧碰撞
- git - 连接到存储库时出现 Jenkins-Git 集成问题