我有这个HTML:
<div class="hello top">some content</div>
<div class="hello top">some content</div>
<div class="hello">some content</div>
<div class="hello">some content</div>
<div class="hello">some content</div>
...我试图只获得那些有“hello”类而不是“top”类的DIV(我想要获得3个最后的DIV)。
我试过这样的事但没有成功:
foreach( $html->find('div[class="hello"], div[class!="top"]') as $element ) {
// some code...
}
答案 0 :(得分:2)
使用此方法:
var result = $("div:not(.top)");
console.log(result);
//只会获得包含“hello”类的DIV。
答案 1 :(得分:0)
根据此表(Supports these operators in attribute selectors):
Filter Description
[attribute] Matches elements that have the specified attribute.
[!attribute] Matches elements that don't have the specified attribute.
[attribute=value] Matches elements that have the specified attribute with a certain value.
[attribute!=value] Matches elements that don't have the specified attribute with a certain value.
[attribute^=value] Matches elements that have the specified attribute and it starts with a certain value.
[attribute$=value] Matches elements that have the specified attribute and it ends with a certain value.
[attribute*=value] Matches elements that have the specified attribute and it contains a certain value.
您可以使用:
foreach( $html->find('div[class$="hello"]') as $element ) {
// some code...
}
但这不是可靠的解决方案,因为它也匹配:
<div class="top hello">
答案 2 :(得分:0)
这样您就可以选择3个最新的“hello”类名。
<html>
<header>
</header>
<body>
<?php
$html= '
<div class="hello top">some content</div>
<div class="hello top">some content</div>
<div class="hello">ee some content</div>
<div class="hello">ee some content</div>
<div class="hello">ee some content</div>';
$dom = new DomDocument();
$dom->loadHTML($html);
$dom_xpath = new DOMXpath($dom);
$elements = $dom_xpath->query('//div[@class="hello"]');
foreach($elements as $data){
echo $data->getAttribute('class').'<br />';
}
?>
</body>
</html>
答案 3 :(得分:0)
[attribute $ = value]匹配具有指定属性的元素,并以特定值结束。 在您的情况下使用
time_since_epoch