简体   繁体   English

使用SimpleHTMLDOM获取类属性

[英]Get class attribute using SimpleHTMLDOM

I am trying to get the value of the attribute class using the following code 我正在尝试使用以下代码获取属性class的值

foreach($sub->children() as $child){
    if($child->class!=="viewAll"){
        echo $child->plaintext."<br>";
    }
}

I am unable get the class value. 我无法获得课程价值。 How can I achieve this? 我该如何实现?

update I am traversing through this source. 更新我正在遍历此源。 Am I wrong in logic? 我的逻辑错了吗?

在此处输入图片说明

From your code, I assume you are looking for <li> elements without the class viewAll . 从您的代码中,我假设您正在寻找没有类viewAll <li>元素。

You may need to check whether the class attribute exists first. 您可能需要检查class属性是否首先存在。 Try this: 尝试这个:

foreach($sub->children() as $child){
    # child element doesn't have a class attrib
    if (! $child->hasAttribute('class') || 
    # or child element has a class but it is not 'viewAll'
       ($child->hasAttribute('class') && $child->getAttribute('class') !== 'viewAll'))
        echo $child->plaintext."<br>";
    }
}

Or, in the simple-html-dom style, this: 或者,在simple-html-dom样式中,这是:

$lis = $html->find('ul li[!class]');
foreach ($lis as $li) {
    echo $li->plaintext."<br>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM