Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要使用Javascript在表格高度中设置div的帮助吗_Javascript_Html_Html Table - Fatal编程技术网

需要使用Javascript在表格高度中设置div的帮助吗

需要使用Javascript在表格高度中设置div的帮助吗,javascript,html,html-table,Javascript,Html,Html Table,我试图将div设置为空表单元格内的某个高度,以将单元格设置为100%高度。(整个页面只包含表格,没有填充。) 以下是一些相关代码: <table border="0" width="100%"> <tr> <td class="boxmiddleleft"></td> <td class="boxmiddlecenter"><script language="javascript" src="includes/clock

我试图将div设置为空表单元格内的某个高度,以将单元格设置为100%高度。(整个页面只包含表格,没有填充。)

以下是一些相关代码:

<table border="0" width="100%">
 <tr>
  <td class="boxmiddleleft"></td>
  <td class="boxmiddlecenter"><script language="javascript" src="includes/clock.js">/*clock*/</script></td>
 </tr>
 <tr>
  <td class="boxbottomleft"><script language="javascript" src="includes/windowsize.js"></script><div id="divbottomresize"></div></td>
  <td class="boxbottomcenter"><button type="button" onclick="resizeheight();">Changed the window height? Reset by pressing this button.</button></td>
 </tr>
</table>
如果你知道我做错了什么,请告诉我。或者,如果你发现其他代码,做什么我想自由粘贴或链接我。(如果可能的话,我不想使用jQuery。)

当我在Chrome中运行代码并转到“Inspect Element”时,我得到错误:

windowsize.js:12未捕获类型错误:无法读取行元素上未定义的属性“style”。style.height=((height-element.offsetTop)+“px”)


编辑:我已更改
var element=“divbottomresize”
to
var element=document.getElementById(“divbottomresize”)如某人建议的那样。这仍然不起作用,我得到相同的错误,并且没有高度更改。

调整大小功能需要元素参数,因此我们需要更新HTML:

 <td class="boxbottomcenter"><button type="button" onclick="resizeheight('divbottomresize');">Changed the window height? Reset by pressing this button.</button></td>

感谢您的帮助,我在中添加了此更改,因为这是一个错误,但当我转到chrome上的“Inspect element”选项时,仍然会出现错误“element.style.height=((height-element.offsetTop)+“px”);”。div仍然无法调整大小。脚本位于何处?检索div时可能没有加载该页面。让我们改为尝试:
document.getElementById(“divbottomresize”).style.height=((height-element.offsetTop)+“px”)我现在看到了另一个问题:您创建的函数有一个参数元素。由于调用函数时没有任何参数,因此未设置该参数。然后,未读取在函数外部定义的变量,因为它与函数参数具有相同的名称。请参阅更新的答案。脚本存储在
includes/window.js
中,因此不在主文件中。我尝试过你的代码更改,但仍然没有更改。我不再得到一个错误,这是一个开始,但什么也没有发生。很奇怪。。。谢谢你的帮助。更新,我修改了你的代码并使用了
document.getElementById(“divbottomresize”).style.height=((height-document.getElementById(“divbottomresize”).offsetTop)+“px”)这将调整div的大小!然而,它并没有去除上面的额外高度(或者它收集了错误的窗户高度,我不知道)。结果是div大约是我的窗口高度。谢谢你迄今为止的帮助。
 <td class="boxbottomcenter"><button type="button" onclick="resizeheight('divbottomresize');">Changed the window height? Reset by pressing this button.</button></td>
function resizeheight(id_element) {
 var div = document.getElementById(id_element);

 var height = 0;
 var body = window.document.body;
 if (window.innerHeight) {
  height = window.innerHeight;
 } else if (body.parentElement.clientHeight) {
  height = body.parentElement.clientHeight;
 } else if (body && body.clientHeight) {
  height = body.clientHeight;
 }
 div.style.height =   ((height - div.offsetTop) + "px");
}