2 回答

TA贡献1825条经验 获得超6个赞
由于您正在更改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;
}
由于您在单击不同的按钮时调用相同的函数,因此它们将具有相同的行为。因此,如果您想以编程方式执行此操作,您将需要添加一个新函数或使用奇怪的逻辑使已经庞大的函数变得过于复杂。
弹出的真正问题是:你为什么想要这种行为?

TA贡献1876条经验 获得超7个赞
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。
添加回答
举报