我想在屏幕上打印 JavaScript 函数的值。我怎么做?抱歉,我是 JavaScript 新手。这就是我到目前为止所做的...
< button onclick="eng += 1; myFunction(eng,'Engaged: ');">Engaged< /button> // i wanna print the value of engaged here.
var eng = 0;
function myFunction( i,txt ){
var plural;
if ( i != 1 ) plural = "s.";
else plural = ".";
alert(txt + i + " time" + plural);
}
每次点击时,engagement 的值都会增加,并显示在警报框中。但我想把它展示在按钮附近。有办法做到吗?谢谢。
最佳答案
输入类似 <span id="spanID"></span>
的内容在您的按钮旁边
然后使用以下 JavaScript
document.getElementById('spanID').innerHTML = txt + i + " time" + plural";
关于javascript - 显示 javascript 函数的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5812487/