仅使用 JavaScript 单击按钮即可增加字体大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38627822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Increase the font size with a click of a button using only JavaScript
提问by canecorso
I'm attempting to increase the font size with the click of a button. I have got the first one to work but I can't get my head around the second one. The second one, every click will increase the font by 1px (I'm pretty much stuck):
我试图通过单击按钮来增加字体大小。我已经让第一个工作了,但我无法理解第二个。第二个,每次点击都会使字体增加 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>
回答by Lucas Dias
Change your function to as in the code below and see what happens:
将您的函数更改为如下代码所示,看看会发生什么:
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';
}
Note: as you can see, there are a lot of duplication in both functions. Therefore, if I were you, I would change this function to increase the fontsize by a given value(in this case, an int).
注意:如您所见,这两个函数都有很多重复。因此,如果我是你,我会更改此函数以将 fontsize 增加给定值(在本例中为 int)。
So, what we can do about it? I think we should turn these functions into a more generic one.
那么,我们能做些什么呢?我认为我们应该把这些功能变成一个更通用的功能。
Take a look at the code below:
看看下面的代码:
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';
}
Now, for example, in your button "Increase Font Size 1px", you should put something like:
现在,例如,在您的按钮"Increase Font Size 1px" 中,您应该输入以下内容:
<input type="button" value="Increase Font Size 1px" onclick="increaseFontSize("b", 1)">
<p id="b">Font Size by 1 Pixel</p>
But, if we want a "Decrease Font Size 1px", what we can do? We call the function with -1 rather than with 1.
但是,如果我们想要一个"Decrease Font Size 1px",我们能做什么?我们用 -1 而不是 1 来调用函数。
<input type="button" value="Decrease Font Size 1px" onclick="increaseFontSize("b", -1)">
<p id="b">Font Size by -1 Pixel</p>
We solve the Decrease Font Size problemas well. However, I would change the function name to a more generic one and call it in both two functions that I would create: increaseFontSize(id, increaseFactor)and decreaseFontSize(id, decreaseFactor).
我们也解决了减小字体大小的问题。但是,我会将函数名称更改为更通用的名称,并在我将创建的两个函数中调用它:increaseFontSize(id, increaseFactor)和reductionFontSize(id, reductionFactor)。
That's it.
就是这样。
回答by Jacoby Yarrow
Try this:
尝试这个:
<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>
回答by Rinto Antony
Simple query
简单查询
Font Size
字体大小
<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>
回答by Walter de Jong
Cycle through different font sizes by clicking on an element
通过单击元素在不同的字体大小之间循环
$(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>
回答by chaman gupta
$(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>
回答by Mamunur Rashid
const button = document.querySelector('.bigger');
button.addEventListener('click', function(e) {
const newFontSize =
parseFloat(getComputedStyle(e.currentTarget).fontSize) + 1;
e.currentTarget.style.fontSize = `${newFontSize}px`;
});