html按钮和javascript代码
嗯。我做了一个计算2的幂的程序。我有html代码。但我先给你描述一下。有一个id='inputin'的输入,下面有一个按钮。在输入中写一个数字。此数字将是2的指数,按下按钮后,将出现一个带有结果的console.log窗口 以下是html:html按钮和javascript代码,javascript,html,button,onclick,Javascript,Html,Button,Onclick,嗯。我做了一个计算2的幂的程序。我有html代码。但我先给你描述一下。有一个id='inputin'的输入,下面有一个按钮。在输入中写一个数字。此数字将是2的指数,按下按钮后,将出现一个带有结果的console.log窗口 以下是html: <div id='main'> <input type="number" name="power2" placeholder='Enter the power' id='inputin'><br/> <
<div id='main'>
<input type="number" name="power2" placeholder='Enter the power' id='inputin'><br/>
<button id="submit" onclick="binaryS()">Do it!</button>
</div>
做吧!
还有javascript:
binaryS = function() {
x = document.getElementById("inputin").value;
y=1;
for(i=1;i<=x;i++) {
y=y*2;
}
console.log (y);
};
binaryS=function(){
x=document.getElementById(“inputin”).value;
y=1;
对于(i=1;i您的JavaScript应该是:
function binaryS() {
var x = document.getElementById("inputin").value;
document.getElementById("outputValue").innerHTML = Math.pow(2,x);
}
将以下内容放置在页面上的某个位置以便显示:
<div id="outputValue">Result</div>
结果
尝试此功能:
function nearestPow2(){
return Math.pow(2, Math.round(Math.log(document.getElementById("inputin").value;) / Math.log(2)));
}
Ref:没有详细说明,您的代码正在运行。我制作了一个html页面来演示它:
<html>
<head>
<script type="application/x-javascript">
binaryS = function() {
x = document.getElementById("inputin").value;
y=1;
for(i=1;i<=x;i++) {
y=y*2;
}
document.getElementById('result').innerHTML = y
};
</script>
</head>
<body>
<div id='main'>
<input type="number" name="power2" placeholder='Enter the power' id='inputin'><br/>
<button id="submit" onclick="binaryS()">Do it!</button>
</div>
<div id="result" ></div>
</body>
</html>
我会使用alert(y)
进行快速检查(没什么特别的)你每次都会得到与y=1相同的答案吗?这对我很有效。似乎效果很好:你明白console.log()吗
不会自动打开窗口吗?你必须使用开发人员工具手动查看日志。+1在我构建日志时击败了我,这基本上是一样的,只使用span
和innerText
。并且不使用数学
:p这是我的第一个真正答案:D感谢这个可爱的网站,我很少有问题问!
Math.pow( 2, parseInt(document.getElementById("inputin").value));