【问题标题】:Resize font with jQuery/JS and override font-size使用 jQuery/JS 调整字体大小并覆盖字体大小
【发布时间】:2011-12-26 20:47:33
【问题描述】:

例如,如果我有:

<div id="divtochangeFont">
    <p>This will change font size</p>
    <p stlye="font-size: 10px">This will not change font size</p>
</div>

现在我想在所有 div 内容中使用 jQuery 调整字体大小。例如,使用简单的 jQuery:

$("#divtochangeFont").css("font-size","16px");

但它只在第一段发生变化。如何覆盖div中所有定义的字体大小属性?

PS:无法使用缩放效果...

【问题讨论】:

  • 你试过 $("#divtochangeFont p").css("font-size","16px"); ?
  • 如果您的浏览器将 stlye 解释为 style,我会担心。
  • 是的,您只需要添加一个 p :jsfiddle.net/srGxF

标签: javascript jquery font-size


【解决方案1】:
$("#divtochangeFont * ").css({"font-size": "16px","color": "orange"});

你只需要带上你的div的所有孩子

【讨论】:

  • 如果没有孩子怎么办 - 只是文字?
  • 如果没有孩子 $("#divtochangeFont, #divtochangeFont * ").
【解决方案2】:
$("#divtochangeFont, #divtochangeFont *").css("font-size", "16px");

【讨论】:

    【解决方案3】:

    你可以这样做:

    $("#divtochangeFont").css("font-size","16px");
    $("#divtochangeFont").children().each(function () {
      $(this).css("font-size","16px");
    });
    

    【讨论】:

      【解决方案4】:

      由于您在父 div 上设置字体,因此字体大小为 10px 的子 p 优先于该段落。 (这就是 CSS 中的级联。)

      frank blizzard 的建议会奏效。就个人而言,如果#divtochangeFont 仅包含 p,我只会更改您的选择器。

      $("#divtochangeFont p").css("font-size","16px");
      

      【讨论】:

        【解决方案5】:

        试试这样,这样你就不用担心其他元素了

        $("#divtochangeFont").addClass("important").css("font-size","16px");
        

        How to apply !important using .css()?

        【讨论】:

          猜你喜欢
          • 2014-04-04
          • 2014-03-27
          • 2018-10-15
          • 2019-04-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多