javascript - 遍历html文件获取aa href
问题描述
我的html文件如下
<div id="sidebar" style="top: 100px;">
<div class="items">
<div class="item hentry selected" itemscope="" itemtype="http://schema.org/BlogPosting" data-id="3714235398193725034">
<img class="thumbnail" src="http://4.bp.blogspot.com/-FLnjwm6youQ/UUGhQei8KqI/AAAAAAAAAUE/nEl-5V5IcDw/s30-p/1.jpg" style="width: 30px; height: 30px;">
<h3 class="title entry-title" itemprop="name">
<a href="http://mywebsiteurl/2013/03/blog-post.html" rel="bookmark" itemprop="url">art1</a>
</h3>
</div>
<div class="item hentry" itemscope="" itemtype="http://schema.org/BlogPosting" data-id="179325489509322215">
.
.
.
</div>
</div>
</div>
html 有一个带有 id 侧边栏的 div
在此之下,还有另一个带有类项目的 div
在此之下有多个带有类项目的div
在每个带有类项目的 div 下,我有一个带有类标题的 h3
在 h3 标签下我有 'a' 标签
我需要在所有具有类项的 div 下获取“a”标签的 href 值。
对于如何做同样的事情,我将不胜感激。
谢谢
解决方案
一旦尝试使用内联 jQuery:
$.each($("#sidebar .items .item h3 a"),function(a,b){console.log($(b).attr("href"));});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="sidebar" style="top: 100px;">
<div class="items">
<div class="item hentry selected" itemscope="" itemtype="http://schema.org/BlogPosting" data-id="3714235398193725034">
<img class="thumbnail" src="http://4.bp.blogspot.com/-FLnjwm6youQ/UUGhQei8KqI/AAAAAAAAAUE/nEl-5V5IcDw/s30-p/1.jpg" style="width: 30px; height: 30px;">
<h3 class="title entry-title" itemprop="name">
<a href="http://mywebsiteurl/2013/03/blog-post.html" rel="bookmark" itemprop="url">art1</a>
</h3>
</div>
<div class="item hentry" itemscope="" itemtype="http://schema.org/BlogPosting" data-id="179325489509322215">
<img class="thumbnail" src="http://4.bp.blogspot.com/-FLnjwm6youQ/UUGhQei8KqI/AAAAAAAAAUE/nEl-5V5IcDw/s30-p/1.jpg" style="width: 30px; height: 30px;">
<h3 class="title entry-title" itemprop="name">
<a href="http://example.com" rel="bookmark" itemprop="url">art2</a>
</h3>
</div>
</div>
</div>
推荐阅读
- python - Kubernetes python客户端:删除缺少的变量
- c++ - 从 const 对象访问非 const 方法
- google-cloud-platform - 使用带有 terraform 的 GCP 云时,我可以自动启用 API 吗?
- google-cloud-platform - Spring Cloud Dataflow 与 Apache Beam/GCP 数据流说明
- vuejs2 - 道具显示在模板中,但不显示在函数中
- angular - 当 URL 太长时,如何防止哈希被清除,例如在 OAuth 令牌场景中,通过 Angular CLI 使用 Webpack-Dev-Server?
- sql-server - 可用性组同步提交 - 写/读问题
- javascript - Impress.js 鼠标滚轮事件
- c++ - Boost [Windows] - 被迫链接额外的库是否正常?
- maple - 给定 n > 2 的过程返回 {2, . . . , n} 大小为 3