【问题标题】:Increase the font size with a click of a button using only JavaScript仅使用 JavaScript 通过单击按钮来增加字体大小
【发布时间】:2016-12-02 08:11:40
【问题描述】:

我正在尝试通过单击按钮来增加字体大小。我有第一个工作,但我无法理解第二个。第二个,每次点击都会增加1px的字体(我卡住了):

<input type="button" value="Increase Font Size 100px" onclick="increaseFontSizeBy100px()">
<p id="a">Font Size</p>

<input type="button" value="Increase Font Size 1px" onclick="increaseFontSizeBy1px()">
<p id="b">Font Size by 1 Pixel</p>

<script> 
    function increaseFontSizeBy100px() {
        document.getElementById('a').style.fontSize = "100px";
    }

    function increaseFontSizeBy1px() {
        var font = document.getElementById('b').style.fontSize;            
        font++;
    }
</script>

【问题讨论】:

  • document.getElementById('b').style.fontSize 是一个字符串,而不是一个可以增加的数字,即使可以,您实际上也不是 设置 字体,您只是增加了一些变量。

标签: javascript button font-size


【解决方案1】:

将您的函数更改为如下代码所示,看看会发生什么:

function increaseFontSizeBy100px() {
    txt = document.getElementById('a');
    style = window.getComputedStyle(txt, null).getPropertyValue('font-size');
    currentSize = parseFloat(style);
    txt.style.fontSize = (currentSize + 100) + 'px';
}
function increaseFontSizeBy1px() {
    txt = document.getElementById('b');
    style = window.getComputedStyle(txt, null).getPropertyValue('font-size');
    currentSize = parseFloat(style);
    txt.style.fontSize = (currentSize + 1) + 'px';
}

注意:如您所见,这两个函数有很多重复之处。因此,如果我是你,我会更改此函数以将字体大小增加给定值(在本例中为 int)。

那么,我们能做些什么呢?我认为我们应该将这些功能变成更通用的功能。

看看下面的代码:

function increaseFontSize(id, increaseFactor){
     txt = document.getElementById(id);
     style = window.getComputedStyle(txt, null).getPropertyValue('font-size');
     currentSize = parseFloat(style);
     txt.style.fontSize = (currentSize + increaseFactor) + 'px';
}

现在,例如,在您的按钮 “增加字体大小 1px” 中,您应该输入如下内容:

<input type="button" value="Increase Font Size 1px" onclick="increaseFontSize("b", 1)">
<p id="b">Font Size by 1 Pixel</p>

但是,如果我们想要一个“减小字体大小 1px”,我们可以做什么?我们用 -1 而不是 1 调用函数。

<input type="button" value="Decrease Font Size 1px" onclick="increaseFontSize("b", -1)">
<p id="b">Font Size by -1 Pixel</p>

我们也解决了减小字体大小问题。但是,我会将函数名称更改为更通用的名称,并在我将创建的两个函数中调用它:increaseFontSize(id, increaseFactor)decreaseFontSize(id, reductionFactor)强>。

就是这样。

【讨论】:

  • 无 JQuery。荣誉。
【解决方案2】:

试试这个:

<input type="button" value="Increase Font Size 100px" onclick="increaseFontSizeBy100px()">
<p id="a">Font Size</p>

<input type="button" value="Increase Font Size 1px" onclick="increaseFontSizeBy1px()">
<p id="b">Font Size by 1 Pixel</p>

<script> 
    function increaseFontSizeBy100px() {
        document.getElementById('a').style.fontSize = "100px";
    }
    function increaseFontSizeBy1px() {
        var id = document.getElementById('b');
        var style = window.getComputedStyle(id, null).getPropertyValue('font-size');
        var currentSize = parseInt(style);
        currentSize++;
        document.getElementById('b').style.fontSize = currentSize.toString();
    }
</script>

【讨论】:

    【解决方案3】:

    通过点击一个元素来循环使用不同的字体大小

    $(document).ready(function() {
      $("#ModelIDBarcode").click(function() {
        newFontSize = incSize($('.barcode').css("font-size"), 10, 5, 120)
        $('.barcode').css("font-size", newFontSize);
      });
    });
    
    function incSize(currentSize, incr, min, max) {
      fSize = (parseFloat(currentSize) + incr) % max + min;
      return (fSize) + 'px';
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <label title="Click to increase size" id="ModelIDBarcode" class="barcode" style="font-family:'3 of 9 Barcode'; font-size:10px; background-color:white">
    *ABarcodeValue*
    </label>

    【讨论】:

      【解决方案4】:
      const button = document.querySelector('.bigger');
      button.addEventListener('click', function(e) {
        const newFontSize =
          parseFloat(getComputedStyle(e.currentTarget).fontSize) + 1;
        e.currentTarget.style.fontSize = `${newFontSize}px`;
      });
      

      【讨论】:

      • 你测试了吗?
      • 虽然此代码 sn-p 可能是解决方案,但包含解释确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
      【解决方案5】:

      简单查询

      字体大小

      <input type="button" value="Increase Font Size 1px" onclick="increaseFontSizeBy1px()">
      <p id="b">Font Size by 1 Pixel</p>
      
      
      <script> 
          function increaseFontSizeBy100px() {
              document.getElementById('a').style.fontSize = "100px";
          }
          function increaseFontSizeBy1px() {
          var font = parseInt($("#b").css('font-size'));
      
          font++;
           document.getElementById('b').style.fontSize =font+ "px";
          }
      </script>
      

      【讨论】:

      • 没有jQuery可以实现吗?
      【解决方案6】:

      $(document).ready(function() {
        $("#ModelIDBarcode").click(function() {
          newFontSize = incSize($('.barcode').css("font-size"), 10, 5, 120)
          $('.barcode').css("font-size", newFontSize);
        });
      });
      
      function incSize(currentSize, incr, min, max) {
        fSize = (parseFloat(currentSize) + incr) % max + min;
        return (fSize) + 'px';
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
      <label title="Click to increase size" id="ModelIDBarcode" class="barcode" style="font-family:'3 of 9 Barcode'; font-size:10px; background-color:white">
      *ABarcodeValue*
      </label>

      【讨论】:

      • 欢迎来到 Stack Overflow!请在您的答案中包含一些细节。您更改了哪些代码来生成此答案?原始海报做错了什么?否则人们将不得不扫描您的代码以获取答案,您可能会投下反对票。
      猜你喜欢
      • 2014-08-06
      • 1970-01-01
      • 2014-09-15
      • 2012-09-12
      • 2018-05-23
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多