将类应用于 DOM 中的父元素?

发布于 2024-10-26 11:52:40 字数 1404 浏览 0 评论 0原文

        <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 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    茶底世界 2024-11-02 11:52:40

    也许你可以尝试这样的事情:

    $parent = $cell->parentNode;
    
    while ($parent->tagName != 'li')
    {
        $parent = $parent->parentNode;
    }
    
    $class = $parent->getAttribute('class');
    $parent->setAttribute('class', $class . ' ' . strtolower($condition));
    

    Maybe you could try somthing like :

    $parent = $cell->parentNode;
    
    while ($parent->tagName != 'li')
    {
        $parent = $parent->parentNode;
    }
    
    $class = $parent->getAttribute('class');
    $parent->setAttribute('class', $class . ' ' . strtolower($condition));
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文