Ant*_*shy 2 html javascript innerhtml
我这样做了:
var blah = document.getElementById('id').getElementsByClassName('class')[0].innerHTML;
Run Code Online (Sandbox Code Playgroud)
现在我在吧:
<a class="title" href="http://www.example.com/" tabindex="1">Some text goes here</a> <span class="domain">(<a href="/domain/foobar.co.uk/">foobar.co.uk</a>)</span>
Run Code Online (Sandbox Code Playgroud)
我想使用JS(没有jQuery)从HTML中读取字符串"Some text goes here".我无法访问该网站的HTML.我正在解析一个网页,为浏览器扩展注入JS.
我是否只需要将其解析为字符串并从>和<之间找到我的文本,或者有没有办法解析JS中的innerHTML?
我假设您拥有的基本HTML标记:
<div id="id">
<div class="class">
<a class="title" href="http://www.example.com/" tabindex="1">Some text goes here</a> <span class="domain">(<a href="/domain/foobar.co.uk/">foobar.co.uk</a>)</span>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
所以选择锚点并阅读文本
var theAnchorText = document.getElementById('id').getElementsByClassName('class')[0].getElementsByTagName("a")[0].textContent;
Run Code Online (Sandbox Code Playgroud)
如果你需要支持IE8
var theAnchor = document.getElementById('id').getElementsByClassName('class')[0].getElementsByTagName("a")[0];
var theAnchorText = theAnchor.textContent || theAnchor.innerText;
Run Code Online (Sandbox Code Playgroud)
如果您使用的是现代浏览器,querySelector会使它更清晰
var theAnchorText = document.querySelector("#id .class a").textContent;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3012 次 |
| 最近记录: |