开发者

add span tag within anchor in jquery

How to add span tag within anchor, changing it from

<a href="开发者_如何学运维somewhere.html">Here is the link to somewhere.</a>

to this with jquery

<a href="somewhere.html">
<span>Here is the span content.</span>
Here is the link to somewhere.
</a>


Try this:

$('a').each(function() {
     $(this).prepend('<span>This is the span content</span>');
});

This will add a span tag to the start of each and every a element on the page. If you want to restrict the modifying to some specific elements, add a class to them and call them with something like $('a.myclass').each(...


Maybe this will work:

$('a').each(function()
{
  $(this).wrapInner("<span></span>");
});

JQuery docs


Add a way to select the link :

<a id="myLink" href="somewhere.html">Here is the link to somewhere.</a>

The do some jQuery :

var $link = jQuery("#myLink");
$link.html( "<span>blah</span>" + $link.html() );

An even cooler way of doing this would be using prepend :

jQuery("#myLink").prepend("<span>blah</span>");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜