1

私はこの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...
}
4

4 に答える 4

2

次の方法を使用します。

var result = $("div:not(.top)");
console.log(result);

//クラス「hello」を含む DIV のみを取得します。

于 2013-09-26T06:56:29.767 に答える
0

[attribute$=value] 指定された属性を持ち、特定の値で終わる要素に一致します。あなたの場合、使用

foreach( $html->find('div[class$="hello"]') as $element ) {
  // some code...
}
于 2015-06-17T05:29:20.773 に答える
0

この表によると (属性セレクターでこれらの演算子をサポートします):

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">
于 2013-09-26T06:45:17.473 に答える
0

このようにして、すでに最新の「hello」クラス名を 3 つ選択できます。

<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>
于 2013-09-26T06:48:06.470 に答える