【问题标题】:jQuery - decrease heading sizejQuery - 减小标题大小
【发布时间】:2017-04-13 18:35:34
【问题描述】:

我想将页面上的所有标题减一

例子:

<h1>Hi</h1> -> <h2>Hi</h2> <h3>Moin</h3> -> <h4>Hi</h4>

在 jQuery 中实现这一目标的最佳方法是什么?

谢谢!

【问题讨论】:

  • 你有什么尝试吗?
  • 您可以添加更多您需要的信息吗?如果你想增加或减少字体大小。在这里试试:stackoverflow.com/questions/24241457/…
  • 实际上我想为自定义类做类似的事情,但意识到使用属性更容易: let headIndex = parseInt($(this).attr("data-head-index") ) || 0;我仍然认为最初的问题总体上可能很有趣。

标签: jquery


【解决方案1】:

可能不是最好的解决方案,但你可以试试这个:

$("h1, h2, h3, h4, h5").each(function() {

  if($(this).is("h1")) {
    $(this).replaceWith("<h2>" + $(this).text() + "</h2>");
  }
  else if($(this).is("h2")){
    $(this).replaceWith("<h3>" + $(this).text() + "</h3>");
  }
  else if($(this).is("h3")){
    $(this).replaceWith("<h4>" + $(this).text() + "</h4>");
  }
  else if($(this).is("h4")){
    $(this).replaceWith("<h5>" + $(this).text() + "</h5>");
  }
  else if($(this).is("h5")){
    $(this).replaceWith("<h6>" + $(this).text() + "</h6>");
  }

});

DEMO

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 2021-04-20
    相关资源
    最近更新 更多