我正在使用:document.write ("temperature is" + c.toString())
在下一页上获取计算结果。我确实了解要将历史记录按钮放回页面中,我需要这样做:
<button onclick="goBack()">Go Back</button>
<script>
function goBack() {
window.history.back();
}
</script>
但是我不知道如何将其放入执行函数时document.write
加载的新页面中。 我不应该使用document.write
吗?或者我如何将其放入我的函数中?
这是我的整个功能:
<script type="text/javascript">
function ToC() {
var f = parseFloat(strIn);
var c = (f - 32) * 5/9;
document.tempform.temp.value = c.toString();
}
document.write("Temperature is " + c.toString());
}
</script>
使用document.write
编写按钮,但随后添加 click 事件。
document.write("<button id='back'>Go Back</button>");
document.getElementById('back').onclick=function(){
window.history.back();
};
下面是一个演示: https://jsfiddle.net/Lxx781dm/
如果你正在获取strIn变量的值,那么唯一需要的更改如下:
<script>
function ToC()
{
var f = parseFloat(strIn);
var c = (f - 32) * 5/9;;
document.tempform.temp.value = c.toString();
document.write("Temperature is " + c.toString())
}
</script>
您还必须注意以下行:
document.tempform.temp.value = c.toString();
此行指出您希望将值保存在具有名称tempform的表单和具有名称temp的字段中,因此您应该在页面中具有以下内容:
<form name="tempform">
<input type="text" name="temp" />
</form>