【问题标题】:add span tag within anchor in jquery在jQuery中的锚点内添加span标签
【发布时间】:2010-01-06 23:06:12
【问题描述】:

如何在锚点中添加跨度标签,将其更改为

<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>

【问题讨论】:

  • 在 jQuery 文档中查找 html() 函数并使用它来更改锚点内容。

标签: jquery


【解决方案1】:

试试这个:

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

这将在页面上每个a 元素的开头添加一个span 标签。如果您想将修改限制为某些特定元素,请向它们添加一个类并使用 $('a.myclass').each(... 之类的名称调用它们

【讨论】:

  • @marcgg,你错了,请在投票前查看手册中prepend 的文档。
  • 是的,我在您发表评论前几秒钟就意识到了这一点。我立即删除了我的反对票
【解决方案2】:

也许这会起作用:

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

JQuery docs

【讨论】:

    【解决方案3】:

    添加选择链接的方式:

    <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>");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多