我试图通过点击按钮来增加字体大小。我有第一个工作,但我无法绕过第二个。第二个,每次点击都会使字体增加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>
答案 0 :(得分:6)
将您的功能更改为以下代码,看看会发生什么:
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';
}
注意:正如您所看到的,这两个功能都有很多重复。因此,如果我是你,我会更改此函数以使给定值增加fontsize(在本例中为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,decreaseFactor)。
就是这样。
答案 1 :(得分:1)
试试这个:
<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>
答案 2 :(得分:0)
简单查询
字体大小
<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>
答案 3 :(得分:0)
$(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 :(得分:0)
$(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>
答案 5 :(得分:0)
const button = document.querySelector('.bigger');
button.addEventListener('click', function(e) {
const newFontSize =
parseFloat(getComputedStyle(e.currentTarget).fontSize) + 1;
e.currentTarget.style.fontSize = `${newFontSize}px`;
});