开发者

replace link with its href using jquery

开发者_JAVA技巧i have a p tag that has many anchor tags and some text. i want to replace each of the anchor tags with its respected href.


I interpret your question that you want to replace the whole tag, not only the contained text, like so: http://jsfiddle.net/xXmWj/1/

You are looking for .replaceWith():

$('p a').replaceWith(function() {
    return $(this).attr('href');
});


Try this:

$('p').find('a').each(function ()
{
    var $this = $(this);
    $this.html($this.attr('href'));
});

Demo


CSS3 solution without using jQuery:

<style type="text/css">
    a > span { display: none; }
    a:after { content: attr(href); }
</style>
<a href="http://www.test.com"><span>Test</span></a>

gracefully degrades for non-CSS3 browsers.


Pretty similar, I know...

$("a").each(function(){
  $(this).text($(this).attr('href'));
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜