提问人:Borja 提问时间:4/18/2020 更新时间:4/18/2020 访问量:199
如果元素有这个或那个类,如何使用 getElementsByTagName()?[复制]
How use getElementsByTagName() if element has this or that class? [duplicate]
问:
我有很多元素,我只想用类“A”或类“B”(不是类“C”的元素)来解释它们。<img>
我想这样做是为了用PHP解析网页。
这是我的代码:
$doc = file_get_html('https://www.example.com');
foreach($doc-> getElementById('content')-> getElementsByTagName('img') as $item){
}
我怎样才能用类来推断.A 或 .乙 ?img
foreach($doc-> getElementById('content')-> getElementsByTagName('img[class="A" OR "B"]') as $item){
}
非常感谢您的帮助!
答:
1赞
A l w a y s S u n n y
4/18/2020
#1
怎么样?find()
foreach($doc->getElementById('content')->find("img[class='A'][class='B']") as $item)
{
// other code goes here
}
评论
0赞
Nigel Ren
4/18/2020
如果你在源代码中查看简单的html dom,你可以看到它是一回事 -function getElementsByTagName($name, $idx=null) {return $this->find($name, $idx);}
0赞
A l w a y s S u n n y
4/18/2020
@NigelRen谢谢你,先生,我不知道!
0赞
Borja
4/18/2020
@AlwaysSunny对不起,但不起作用...... :/
0赞
Borja
4/18/2020
@AlwaysSunny你的代码只有在 img 同时具有两个类时才有效......但在我的情况下,如果有“A”类或“B”类,应该可以工作
0赞
Borja
4/18/2020
@NigelRen建议的代码仅在 img 同时具有两个类时才有效......但在我的情况下,如果有“A”类或“B”类,应该可以工作
评论