簡體   English   中英

如何獲取鏈接內容(innerHTML)並將其添加到其他鏈接中作為標題

[英]How can i get link content (innerHTML) and add it to an other link as title

我有帖子的div內容。 這些帖子位於<li> 我想做的是從item-title類上的鏈接中獲取帖子標題,並將其作為標題添加到item-thumbnail類上的鏈接中。

這是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(); 在每個循環內調用將在每個循環迭代中選擇相同的文本節點。 第二個each循環會將文本設置為每個.item-thumbnail

這應該做您想要的:

$("#popular_posts li").each(function(){
  var pptitle = $(this).find(".item-title a").text();
  $(this).find(".item-thumbnail a").attr("title", pptitle);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM