如何使用简单的html dom获取div内部文本

cj3*_*333 2 html javascript dom

<div style="float: left; margin-top: 10px; font-family: Verdana; font-size: 13px; color: #404040;">innertext</div>
Run Code Online (Sandbox Code Playgroud)

我可以访问没有classidspan使用简单的 html dom php 解析器的 div 的内部文本吗?谢谢。

Ben*_*ank 5

如果样式一致,那么你可以循环遍历文档中的所有div并按样式过滤它们。

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
}
Run Code Online (Sandbox Code Playgroud)