开发者

Jquery: Move the focus of an <a> tag from a single word to the entire text <div>

Would appreciate any pointers regarding which function to use for this:

Say I have something like this:

<div class="myTweet">Check out this awesome link <a href="http://bit.ly/uglylookinglink>http://bit.ly/uglylookinglink</a></div>

Which reads:

Check out my awesome link http://bit.ly/uglylookinglink

All I want to do is move the tag to the front of the containing div so that the whole sentence becomes the link, and remove the horrible http://bitl.y link that comes with it.

So it would look like this afterwards:

<div class="myTweet"><a href="http://bit.ly/uglylookinglink>Check out开发者_JAVA百科 this awesome link </a></div>

Which will then read:

Check out my awesome link

It's probably really easy but i cant find the right way to go.

Thanks, Marc


You can use .wrapInner() and the <a> itself to do so, like this:

​$(".myTweet a")​​​​​​​​​​​​​.each(function() {
  $(this).empty().parent().wrapInner(this);
});​

You can give it a try here, this takes the link, removes it original contents via .empty(), then wraps the <div>'s text using the anchor element itself.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜