0 html javascript onclick button
好.我制作了一个计算2的幂的程序.我有html代码.但我先为你描述一下.有一个输入id ='inputin',下面有一个按钮.在输入中你写了一个数字.这个数字将是2的指数,按下按钮后会出现一个带有结果的console.log窗口
这里的html:
<div id='main'>
<input type="number" name="power2" placeholder='Enter the power' id='inputin'><br/>
<button id="submit" onclick="binaryS()">Do it!</button>
</div>
Run Code Online (Sandbox Code Playgroud)
和javascript:
binaryS = function() {
x = document.getElementById("inputin").value;
y=1;
for(i=1;i<=x;i++) {
y=y*2;
}
console.log (y);
};
Run Code Online (Sandbox Code Playgroud)
为什么它不起作用?有帮助吗?如果你可以建议而不是一个丑陋的console.log窗口我怎么能在页面某处的新div中出现?日Thnx
小智 7
你的JavaScript应该是:
function binaryS() {
var x = document.getElementById("inputin").value;
document.getElementById("outputValue").innerHTML = Math.pow(2,x);
}
Run Code Online (Sandbox Code Playgroud)
将以下内容放在页面上以供显示:
<div id="outputValue">Result</div>
Run Code Online (Sandbox Code Playgroud)