how to use simple html dom get a div inner text

前端 未结 4 1871
無奈伤痛
無奈伤痛 2020-12-06 23:39
innertext

Jow can I access i

相关标签:
4条回答
  • 2020-12-06 23:56

    If the styles are consistent, then you can loop over all divs in the document and filter them by style.

    var divs = document.getElementsById("div");
    
    for (var i = 0; i < divs.length; i++) {
        var div = divs[i];
    
        // skip the current div if its styles are wrong
        if (div.style.cssFloat !== "left"
         || div.style.marginTop !== "10px"
         || div.style.fontFamily !== "Verdana"
         || div.style.fontSize !== "13px"
         || div.style.color !== "#404040") continue;
    
        var text = div.innerText || div.textContent;
    
        // do something with text
    }
    
    0 讨论(0)
  • 2020-12-07 00:05

    Thanks to all. I depends on simple_html_dom too much, Ben Blank give me a good way. And I also tried php regular-expression to match the div by myself.

    preg_match_all('/<div.*(style="float: left; margin-top: 10px; font-family: Verdana; font-size: 13px; color: #404040;").*>([\d\D]*)<\/div>/iU',$html,$match);
    print_r($match); 
    
    0 讨论(0)
  • 2020-12-07 00:11

    You may use the content of style tag if no id or class is given there like:

    include('simple_html_dom.php');
    $html = file_get_html('http://www.mysite.com/');
    foreach($html->find('div[style="float: left; margin-top: 10px; font-family: Verdana; font-size: 13px; color: #404040;"]') as $e)
    echo $e->innertext;
    
    0 讨论(0)
  • 2020-12-07 00:11

    You could probably try to match some of their parents (which have class or id set), then traverse the DOM to the child you want.

    0 讨论(0)
提交回复
热议问题