在jquery中的锚点中添加span标记

时间:2010-01-06 14:53:51

标签: jquery

如何在锚点中添加span标记,从

更改它
<a href="somewhere.html">Here is the link to somewhere.</a>

用jquery

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

3 个答案:

答案 0 :(得分:5)

试试这个:

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

这会将span标记添加到页面上每个a元素的开头。如果要将修改限制为某些特定元素,请向其中添加一个类,并使用$('a.myclass').each(...

之类的方法调用它们

答案 1 :(得分:4)

也许这会奏效:

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

JQuery docs

答案 2 :(得分:2)

添加一种选择链接的方法:

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

做一些jQuery:

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

更酷的方法是使用prepend

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