如何获取链接内容(innerHTML)并将其作为标题添加到其他链接
How can i get link content (innerHTML) and add it to an other link as title
我有帖子的div内容。这些帖子存在于<li>
中。我想做的是从项目标题类上的链接中获取帖子标题,并将其作为标题添加到项目缩略图类的链接中。
这是html库。
<div class='section' id='popular_posts'>
<div class='widget PopularPosts' id='PopularPosts1'>
<h2>Popular Posts</h2>
<div class='widget-content popular-posts'>
<ul>
<li>
<div class='item-content'>
<div class='item-thumbnail'>
<a href='http://test-abdelghafour.blogspot.com/2013/04/blog-post.html' target='_blank'>
<img alt='' border='0' height='72' src='http://2.bp.blogspot.com/-uDob3fWnp6s/UV3LNbgvp5I/AAAAAAAAAVo/9cVpf154Dao/s72-c/Koala.jpg' width='72'/>
</a>
</div>
<div class='item-title'><a href='http://test-abdelghafour.blogspot.com/2013/04/blog-post.html'>the first post title</a></div>
<div class='item-snippet'>loprem ipsum loprem ipsum loprem ipsum loprem ipsum loprem ipsum loprem ipsum loprem ipsum </div>
</div>
<div style='clear: both;'></div>
</li>
<li>
<div class='item-content'>
<div class='item-thumbnail'>
<a href='http://test-abdelghafour.blogspot.com/2013/03/blog-post_5093.html' target='_blank'>
<img alt='' border='0' height='72' src='http://2.bp.blogspot.com/-rGSydPkoB1Y/UVXpS-SO9ZI/AAAAAAAAAKM/7kTPOiebTlc/s72-c/Lighthouse.jpg' width='72'/>
</a>
</div>
<div class='item-title'><a href='http://test-abdelghafour.blogspot.com/2013/03/blog-post_5093.html'>the second post title</a></div>
<div class='item-snippet'>loprem ipsum loprem ipsum loprem ipsum loprem ipsum loprem ipsum loprem ipsum loprem ipsum loprem ipsum </div>
</div>
<div style='clear: both;'></div>
</li>
<li>
<div class='item-content'>
<div class='item-thumbnail'>
<a href='http://test-abdelghafour.blogspot.com/2013/03/blog-post_5192.html' target='_blank'>
<img alt='' border='0' height='72' src='http://2.bp.blogspot.com/-hwvXxxnUkEI/UVXinrWBGrI/AAAAAAAAAJ8/3JJc9wbSSLA/s72-c/Tulips.jpg' width='72'/>
</a>
</div>
<div class='item-title'><a href='http://test-abdelghafour.blogspot.com/2013/03/blog-post_5192.html'>the third post title</a></div>
<div class='item-snippet'>loprem ipsum loprem ipsum loprem ipsum loprem ipsum loprem ipsum loprem ipsum loprem ipsum loprem ipsum </div>
</div>
<div style='clear: both;'></div>
</li>
<li>
<div class='item-content'>
<div class='item-thumbnail'>
<a href='http://test-abdelghafour.blogspot.com/2013/03/blog-post_29.html' target='_blank'>
<img alt='' border='0' height='72' src='http://4.bp.blogspot.com/-4hpIKXd5iXw/UVXuIC1mCQI/AAAAAAAAAKs/QiMMwRRl0ns/s72-c/Desert.jpg' width='72'/>
</a>
</div>
<div class='item-title'><a href='http://test-abdelghafour.blogspot.com/2013/03/blog-post_29.html'>the forth post title</a></div>
<div class='item-snippet'>loprem ipsum loprem ipsum loprem ipsum loprem ipsum loprem ipsum loprem ipsum loprem ipsum loprem ipsum </div>
</div>
<div style='clear: both;'></div>
</li>
</ul>
</div>
</div>
</div>
这是我使用的jQuery代码。
$('#popular_posts .item-title a').each(function(){
var pptitle = $('#popular_posts .item-title a').text();
$('.item-thumbnail a').each(function(){
$(this).attr('title', pptitle);
});
});
我想得到的每个链接的结果。
<a href='http://test-abdelghafour.blogspot.com/2013/04/blog-post.html' target='_blank' title="the forth post title">
每个循环内的var pptitle = $('#popular_posts .item-title a').text();
调用将在每个循环迭代中选择相同的文本节点。第二个每个循环将该文本设置为每个.item-thumbnail
。
这应该做你想做的:
$("#popular_posts li").each(function(){
var pptitle = $(this).find(".item-title a").text();
$(this).find(".item-thumbnail a").attr("title", pptitle);
});
相关文章:
- 将超链接添加到“;标题“;标记文本
- 正在尝试将标题标记添加到mailto链接,正文中包含URL
- 链接悬停css在不浮动时不在表浮动标题中工作
- 如何点击标题中的链接,然后返回HTML、Javascript和CSS
- 用新的jQuery和jQuery ui替代.tabs(“添加”、链接、标题)
- 如何获取链接内容(innerHTML)并将其作为标题添加到其他链接
- 基础标题手风琴上的内部链接
- 试图在CSS中以顶部标题居中链接,但不会移动
- 使用带有哈希标签的链接处理类似Facebook的按钮信息(标题和图像)
- 单击链接时如何更改文档标题
- 为什么我的某些链接标题被放置在我的 标签之外
- 检查页面标题是否包含在链接属性中,并使用jQuery分配活动类
- 我想更改wordpress标题中的脚本链接.它们在哪里定义
- 如何使用javascript从类中的链接获取标题属性
- 点击获取链接标题
- 添加链接标题,如果<a>标签
- 将文本附加到悬停时的链接标题
- 将文章中的每个图像都用“;href"父链接标题的
- 花式框pinterest按钮 - 如何将链接标题传递给引脚描述
- 如何在 AngularJS 中更改链接标题