You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用jQuery将button按钮的文本/值包裹在span标签内

jQuery实现方案

以下两种方案可根据实际场景选择:

  • 基础场景方案
    你给出的按钮有专属类名single_add_to_cart_button,直接选中后调用wrapInner方法即可完成包裹:
$(document).ready(function() {
  $('button.single_add_to_cart_button').wrapInner('<span></span>');
});
  • 多按钮场景方案
    如果页面存在多个同类名的按钮,可以增加文本过滤逻辑,只处理文本为「Buy Now」的按钮,避免误操作:
$(document).ready(function() {
  $('button.single_add_to_cart_button').filter(function() {
    return $(this).text().trim() === 'Buy Now';
  }).wrapInner('<span></span>');
});

最终效果

代码执行后,你的按钮结构会变成如下格式:

<button type="submit" name="add-to-cart" value="22706" class="single_add_to_cart_button button alt">
  <span>Buy Now</span>
</button>

内容的提问来源于stack exchange,提问作者Briany Hearne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 23:45:10