需要帮助使用 Javascript 设置表格高度中的 div
我试图将 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>
我尝试更改名为“#divbottomresize”的 div 高度)
以及我为此使用的 JavaScript:
var element = "divbottomresize";
function resizeheight(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;
}
element.style.height = ((height - element.offsetTop) + "px");
}
如果您知道什么我做错了请你告诉我。或者,如果您发现其他代码可以实现我想要的功能,请随时粘贴或链接我。 (如果可能的话,我不想使用 jQuery。)
当我在 Chrome 中运行代码并转到“检查元素”时,出现错误:
windowsize.js:12Uncaught TypeError: 无法读取行上未定义的属性“style” element.style.height = ((height - element.offsetTop) + "px");
编辑:我已将 var element = "divbottomresize";
更改为 var element = document.getElementById("divbottomresize");
正如有人建议的那样。这仍然不起作用,我得到同样的错误并且高度没有变化。
I am trying to set the div to a certain height inside an empty table cell to set the cell to 100% height. (The whole page consists of just the table with no padding.)
Here is some relevant code:
<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>
(With the div im trying to change height called '#divbottomresize')
And the JavaScript I am using for this:
var element = "divbottomresize";
function resizeheight(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;
}
element.style.height = ((height - element.offsetTop) + "px");
}
If you have any idea what I am doing wrong please could you tell me. Alternatively if you find other code that does what I want feel free to paste or link me. (I do not want to use jQuery if possible.)
When I run the code in Chrome and go to 'Inspect Element' I get the error:
windowsize.js:12Uncaught TypeError: Cannot read property 'style' of undefined on the line element.style.height = ((height - element.offsetTop) + "px");
EDIT: I have changed var element = "divbottomresize";
to var element = document.getElementById("divbottomresize");
as someone helpfully suggested. This still does not work and I get the same error and no height change.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调整大小函数需要 element 参数,因此我们需要更新 HTML:
现在,我们需要更改 Javascript,以使用函数传递的 STRING 值,然后检索具有该 ID 的标签以进行进一步处理:
The resize function requires the element parameter, so we need to update the HTML:
Now, we need to change the Javascript, to use the STRING value passed by the function and then retrieve the tag with that ID for further processing: