将类应用于 DOM 中的父元素?
<li id="weather" class="widget-container widget-dark-blue">
<h3 class="widget-title">Weather</h3>
<?php include (TEMPLATEPATH . '/widgets/weather.php'); ?>
</li>
Weather.php 向天气服务发出一个curl 请求并返回一个表。使用 DomDocument 类,我读取了 td 内部的值。我将当前天气状况的类名应用于 div.weather
。
<?php
require_once('classes/SmartDOMDocument.class.php');
$url = 'some/domain...';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$str = curl_exec($curl);
$dom = new SmartDOMDocument();
$dom->loadHTML($str);
$xpath = new DOMXPath($dom);
$tds = $xpath->query('//div/table/tr/td');
foreach ($tds as $key => $cell) {
if ($key==1) {
$condition = $cell->textContent;
//$cell->parentNode->setAttribute('class', 'hello');
echo "<div class='weather " . strtolower($condition) ."'>";
...
?>
一切正常。我唯一的问题是,是否有一种 PHP 方法可以将类名 $condition 应用于保存信息的列表项? 因此,我不想在 li#weather 中使用 $condtion 类,而是希望在类中使用 li#weather 。
有什么方法可以将 $condition 类应用到包含所有内容的列表中。我可以用 javascript/jquery 轻松做到这一点。不过我想知道是否有一些服务器端解决方案。
谢谢
<li id="weather" class="widget-container widget-dark-blue">
<h3 class="widget-title">Weather</h3>
<?php include (TEMPLATEPATH . '/widgets/weather.php'); ?>
</li>
weather.php does a curl request to a weather service and returns a table. With the DomDocument Class I read the values inside of the td's
. I'm applying a classname of the current weather condition to a div.weather
.
<?php
require_once('classes/SmartDOMDocument.class.php');
$url = 'some/domain...';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$str = curl_exec($curl);
$dom = new SmartDOMDocument();
$dom->loadHTML($str);
$xpath = new DOMXPath($dom);
$tds = $xpath->query('//div/table/tr/td');
foreach ($tds as $key => $cell) {
if ($key==1) {
$condition = $cell->textContent;
//$cell->parentNode->setAttribute('class', 'hello');
echo "<div class='weather " . strtolower($condition) ."'>";
...
?>
Everything works fine. My one and only question is, is there a PHP way of applying the classname $condition to the list-item that holds the information?
So instead of having a class with the $condtion inside of my li#weather I'd like to have the li#weather the class.
<li id="weather" class="widget-container widget-dark-blue $condition">
Is there any way I can apply the $condition class to the list that hold's everything. I could easily do it with javascript/jquery. However I wonder if there is some serverside solution.
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许你可以尝试这样的事情:
Maybe you could try somthing like :