【问题标题】:Get text of the div that contains more than one characters [closed]获取包含多个字符的 div 文本 [关闭]
【发布时间】:2014-10-06 21:55:40
【问题描述】:

假设我们想要获取 HTML 页面的 div,span,p,etc,该页面的 classID 名称包含一些关键字,如:

one & two & three

例如我有这个 HTML:

<div class="this_one">
  Need this !
</div>
<div id="some_three">
  Need this again !
</div>
<span id="two_this">
  Need again
</span>
<p class="NOT">
Not want this
</p>

我的意思是我想在特殊标签内获取文本,例如 (div,p,span),它们的 ID 或 CLASS 包含我的单词,例如 (one,two,...)

如何检测它们?

例如,使用 simpledomPHPDOM 或任何您想要的方式。

【问题讨论】:

    标签: php html css dom simpledom


    【解决方案1】:

    如果你想使用 PHP,那么你可以使用DOMDocument + DOMXpath。我不是xpath 的大师,但你可以这样做:

    $sample_markup = '<div class="this_one">
      Need this !
    </div>
    <div id="some_three">
      Need this again !
    </div>
    <span id="two_this">
      Need again
    </span>
    <p class="NOT">
    Not want this
    </p>';
    
    $dom = new DOMDocument();
    $dom->loadHTML($sample_markup);
    $xpath = new DOMXpath($dom);
    
    $search = $xpath->query('
        /html/body//*[
            contains(@class|@id, "one") or
            contains(@class|@id, "two") or
            contains(@class|@id, "three")
        ]
    ');
    
    foreach($search as $node) {
        $value_inside_that_node = trim((string) $node->nodeValue);
        echo $value_inside_that_node . '<br/>';
    }
    

    应该输出:

    Need this !
    Need this again !
    Need again
    

    【讨论】:

      【解决方案2】:

      使用 jquery,您可以使用 [name*="value"] 选择器来完成,例如:

      var patterns = ["one","two","three"];
      
      var text = "";
      for(i in patterns) {
          text += $("[id*='"+patterns[i]+"']").text();
          text += $("[class*='"+patterns[i]+"']").text();
       }
      
      alert(text);
      

      DEMO FIDDLE

      【讨论】:

        【解决方案3】:

        你可以使用JQuery's selectors:

        // elements who's class contains one
        $('[class~="one"]').click(function() {
           // do stuff
        });
        

        【讨论】:

          【解决方案4】:

          简单的方法是在页面中获取所有 div、span 或任何其他您想要的标签。检查每个内容以及是否找到匹配项。使用标签的名称或 ID。

          在这个链接上做了一些非常相似的事情 Iterating through all the <div> tags on a page

          也经过

          Parse HTML div id include all inner contents

          PHP DOM parsing to get to elements inside specific div id

          【讨论】:

            【解决方案5】:

            您可以使用 jquery 选择器按其类循环遍历元素,如下所示:

            $("[class=className]").each(function( index ) {
               // do something here
            });
            

            选择多个类:

            $("[class=className1], [class=className2]").each(function( index ) {
               // do something here
            });
            

            这里是 JSFiddle 示例:JSFiddle

            【讨论】:

              猜你喜欢
              • 2022-01-23
              • 2014-10-20
              • 2013-12-20
              • 2014-07-14
              • 1970-01-01
              • 2018-07-04
              • 2018-08-08
              • 1970-01-01
              • 2021-12-17
              相关资源
              最近更新 更多