使用getElementById从获取所有(Get all s from using getElementById)

编程入门 行业动态 更新时间:2024-10-25 10:26:39
使用getElementById using getElementById)

我需要使用PHP从HTML文档中的<select>获取所有<option>数据。 我目前的代码:

$pageData = $this->Http->get($this->config['url']); libxml_use_internal_errors(true); $this->Dom->loadHTML($pageData); $select = $this->Dom->getElementById('DDteam');

我不确定从这里获取每个选项的值以及选项标签内的文本。 我无法使用print_r或类似方法检查对象。

I need to get all of the <option> data from a <select> in an HTML document using PHP. The code that I have at the moment:

$pageData = $this->Http->get($this->config['url']); libxml_use_internal_errors(true); $this->Dom->loadHTML($pageData); $select = $this->Dom->getElementById('DDteam');

I am not sure how, from here, to get the value of each of the options and also the text inside the option tags. I can't inspect the object using print_r or similar either.

最满意答案

您必须使用DOM-API来检索所需的数据。 由于<select>元素不是那么复杂,您可以使用getElementsByTagName获取所有<options> -nodes:

$select = $this->Dom->getElementById('DDteam'); $options = $select->getElementsByTagName('option'); $optionInfo = array(); foreach($options as $option) { $value = $option->getAttribute('value'); $text = $option->textContent; $optionInfo[] = array( 'value' => $value, 'text' => $text, ); } var_dump($optionInfo);

You have to use the DOM-API to retrieve the data you want. Since the <select> element is not that complex you can fetch all the <options>-nodes using getElementsByTagName:

$select = $this->Dom->getElementById('DDteam'); $options = $select->getElementsByTagName('option'); $optionInfo = array(); foreach($options as $option) { $value = $option->getAttribute('value'); $text = $option->textContent; $optionInfo[] = array( 'value' => $value, 'text' => $text, ); } var_dump($optionInfo);

更多推荐

本文发布于:2023-08-04 08:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412602.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:getElementById

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!