Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将类应用于DOM中的父元素?_Php_Domdocument_Classname - Fatal编程技术网

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

Php 将类应用于DOM中的父元素?,php,domdocument,classname,Php,Domdocument,Classname,?> 一切正常。我唯一的一个问题是,是否有一种PHP方法可以将classname$条件应用于保存信息的列表项? 因此,与其在我的li#weather课程中设置$condition,不如让li#weather来上课 是否有任何方法可以将$condition类应用于包含所有内容的列表。我可以用javascript/jquery轻松地完成这项工作。然而,我想知道是否有一些服务器端解决方案 谢谢也许你可以试试这样的东西: <?php require_once('classes/SmartDOM

?>

一切正常。我唯一的一个问题是,是否有一种PHP方法可以将classname$条件应用于保存信息的列表项? 因此,与其在我的li#weather课程中设置$condition,不如让li#weather来上课

  • 是否有任何方法可以将$condition类应用于包含所有内容的列表。我可以用javascript/jquery轻松地完成这项工作。然而,我想知道是否有一些服务器端解决方案


    谢谢

    也许你可以试试这样的东西:

    <?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) ."'>";
            ...
    

    您可以使用
    $node->parentNode
    访问节点的父节点。如果包含的
  • 是直接祖先,那么像
    $cell->parentNode->setAttribute(…)
    这样的东西就可以做到这一点。两者之间什么都没有。但是,例如
    $cell->parentNode->setAttribute('class','hello')不起作用。可能是因为$cell实际上是原始curl请求的
    td
    ,而不是保存此curl请求的列表的子项。
    <?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) ."'>";
            ...
    
    $parent = $cell->parentNode;
    
    while ($parent->tagName != 'li')
    {
        $parent = $parent->parentNode;
    }
    
    $class = $parent->getAttribute('class');
    $parent->setAttribute('class', $class . ' ' . strtolower($condition));