2 回答

TA貢獻1966條經(jīng)驗 獲得超4個贊
由于您正在更改innerHTML(與innerText相對),因此您可以使用文本添加html標記。例如:
JS
button.addEventListener('click', () => changeText('whales', 'snails', '<small>this is the specific text that needs a different font-size</small>'));
button2.addEventListener('click', () => changeText('bees', 'trees', 'this text3 is ok with the actual font-size'));
CSS
#text>small {
font-size: 10px;
}
由于您在單擊不同按鈕時調(diào)用相同的功能,因此它們將具有相同的行為。因此,如果您想以編程方式執(zhí)行此操作,則需要添加新函數(shù)或使用奇怪的邏輯使已經(jīng)龐大的函數(shù)過度復雜化。
彈出的真正問題是:你為什么要這種行為?

TA貢獻1836條經(jīng)驗 獲得超4個贊
var text = document.getElementById('text');
function changeText(text1, text2, text3, button) {
// default behaviour
text.style.color = "red";
if (language == 1) text.innerHTML = text1;
else if (language == 2) text.innerHTML = text2;
else if (language == 3){
text.innerHTML = text3;
// different behaviour
if(button == "button1")
text.style.color = "green";
}
else text.innerHTML = "Don't think about this";
}
<button onclick="changeText('hello','banana','wow','button1');" id="button1">
<button onclick="changeText('vewewve','vdsvs','dgsag','button2');" id="button2">
每次單擊按鈕且語言不是3時,此代碼將默認設置文本。
添加回答
舉報