我有一個textarea,我想在每次點擊按鈕時將其文本字體大小增加2。我寫了這段代碼,但沒有作業。 我的HTML代碼是
。function makebold() {
alert(document. getElementById("input-text").style.fontSize)。)
var currentfontsize = document. getElementById("input-text").style.fontSize;
console.log(currentfontsize)。
var current = parseInt(currentfontsize)。
console.log(current)。
var updatedfontsize = current 2。
console.log(upedfontsize)。
var newfont = updatedfontsize;
console.log(newfont); 。
document.getElementById("input-text"/span>)。 style.fontSize = newfont。
}
<fieldset>
<legend>Text</legend>
< textarea name="input-text" id="input-text" cols="30"/span> rows="4"/span>> </textarea>>
</fieldset>/span>
<fieldset>/span>
<legend>Decoration</legend>
< button id="button1" onclick="makebold()"> Bigger Decoration! </button><br>
< input type="checkbox"/span> onclick="displayalert()" name="bling" id="bling" value="bling"> Bling
</fieldset>/span>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
誰能幫幫我?
uj5u.com熱心網友回復:
使用這種方式來獲得字體大小:
var currentfontsize = window. getComputedStyle(document. getElementById("input-text")).fontsize。
然后更新你的最后一行
document.getElementById("input-text") 。 style.fontSize = newfont 'px'。
你沒有在字體中加入px。沒有px,它就不會作業。
作業原理 https://jsfiddle.net/3napv795/
uj5u.com熱心網友回復:
function increaseFont(elem, inc){
const val = window.getComputedStyle(elem)。 getPropertyValue("font-size"); //獲得字串格式的字體大小。
const currentSize = parseFloat(val); // remove 'px'
elem.style.fontSize = currentSize inc "px"; //設定新尺寸,在當前字體大小上增加增量。
}
使用方法:
const button = document. getElementById("button1"/span>)。
按鈕。 addEventListener("click", () => increaseFont(button, 2))
uj5u.com熱心網友回復:
添加一個初始字體大小將解決這個問題:
function makebold(){
alert(document. getElementById("input-text").style.fontSize)。)
//添加初始字體大小。
document.getElementById("input-text"/span>)。 style.fontSize = '10px'。
var currentfontsize = document. getElementById("input-text").style.fontSize;
console.log(currentfontsize)。
var current = parseInt(currentfontsize)。
console.log(current)。
var updatedfontsize = current 2。
console.log(upedfontsize)。
var newfont = updatedfontsize;
console.log(newfont); 。
document.getElementById("input-text"/span>)。 style.fontSize = newfont。
}
uj5u.com熱心網友回復:
糾正你的代碼,同時更新到一個較新的語法....
。JS
。
const textarea = document.getElementById("input-text") 。
function makebold(/span>) {
let fontUnit = "px"; //為字體大小設定一個單位。
let increment = 2; //增加字體大小2 px/rem/em等等。
//獲取當前字體大小。
let currentfontsize = window.getComputedStyle(textarea).fontSize。
currentfontsize = parseInt(currentfontsize)。
//獲得最終值。
const updatedfontsize = currentfontsize increment fontUnit;
textarea.style.fontSize = updatedfontsize。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/320439.html
標籤:
上一篇:Angular:如何知道所有的子組件View都被啟動了
下一篇:如何訪問此分頁
