页面用力 o>﹏<o 加载中...

table设置height属性不起作用的解决办法



找到的资料如下:

引用内容 引用内容
  最近写JS对table的操作时,发现一个奇怪的现象,对table的width属性进行设置可以改变table的大小,但对table的height属性进行设置时,居然报没有这个属性。代码如下:

复制内容到剪贴板程序代码程序代码
var table = document.getElementById("textTable");  
table.width = 90%;  //这句可以正常执行并改变Table宽度  
table.height = 50%;  //这句无法正常执行,使用alert(table.height);调试可发现,height属性不存在。

  经查,问题应该是在于这个HTML的文件头,我当前使用的文件头是:

复制内容到剪贴板程序代码程序代码
<!DOCTYPE html  
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  

  猜想应该是由于在这个xhtml的dtd里,table没有定义height这个属性,所以不能直接用table.height进行属性设置。
  当然,如果你使用其他的表头,有可能这样的设置方法是可以生效的,更详尽的原因还没时间去深究,希望有知道的高手们能指点一二。这里只给出我自己对于这个问题的解决方法。
  解决方法:根据经验,直接设置属性的方法行不通,可以尝试通过使用setAttribute()方法,居然可以了,代码如下:

复制内容到剪贴板程序代码程序代码
var table = document.getElementById("textTable");  
table.width = 90%;  
table.setAttribute("height",50%);  //这里改成这样设置就解决了



但我用的是直接设定具体数值,这个好像不太适用,而用 document.getElementById("textTable").offsetHeight 可以获得数值,但直接用来加减的话好像又不对,得不到想要的高度

最后用了这个:
复制内容到剪贴板程序代码程序代码
getObject("listbox").setAttribute("height",getObject("menu_tab").offsetHeight-20*4+"px");

具体运算的关系我也不清楚,表格分上下两行,高度是100%的,第二行设定css是20px,执行js时发现运算中-65至80多都可以得到想要的结果,所以就随便取一个了

另还有浏览器窗口最大化、最小化时用到的事件:onresize(),就写在body标签里就可以了。
这个是关闭窗口的事件:onbeforeunload()。
也可以这样用:
复制内容到剪贴板程序代码程序代码
//Written by Milo on Oct 22th,2010
//新增或者修改狀態下離開該畫面提示保存消息
window.onbeforeunload = function(){    
   if(editing)
   {
       return "離開此畫面可能造成尚未保存的資料丟失。";
   }
}




文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: XHTML HTML JavaScript table height
相关日志:
评论: 112 | 引用: 0 | 查看次数: 5809
回复回复gosocial[2022-01-12 06:30 AM | IANA | 成分分析 | del]
Laesosmed - Jasa Tambah Followers, Likes, Views, Instagram
Indonesia
telegram: @laesosmed
gosocial
回复回复galentinos[2022-01-10 02:44 AM | IANA | 成分分析 | del]
Hello There. I found your blog using msn. This is a really well written article.
I'll be sure to bookmark it and come back to read more of your useful info.
Thanks for the post. I will certainly return.
回复回复hdr10이란[2022-01-04 09:12 AM | 俄罗斯 | 成分分析 | del]
It's appropriate time to make a few plans for the future and it's time to be happy.
I have learn this publish and if I could I wish to counsel you few fascinating
issues or advice. Perhaps you can write subsequent articles regarding this article.
I want to read more issues about it!
回复回复seo[2021-10-02 11:07 AM | 香港特别行政区 | 成分分析 | del]
I'm not sure why but this weblog is loading extremely slow for me.
Is anyone else having this issue or is it a issue on my end?
I'll check back later and see if the problem
still exists.
回复回复인계동스웨디시[2021-09-11 06:25 PM | 南非 | 成分分析 | del]
I'm really loving the theme/design of your web site. Do
you ever run into any web browser compatibility problems?
A few of my blog audience have complained about my site not operating correctly in Explorer but looks great in Opera.
Do you have any tips to help fix this issue?
回复回复인계동마사지[2021-09-09 05:36 PM | 南非 | 成分分析 | del]
I really like your blog.. very nice colors & theme.
Did you make this website yourself or did you hire someone to do it for
you? Plz respond as I'm looking to construct my own blog and would like to find out where u got this from.
thanks
回复回复우리카지노[2021-08-29 09:43 PM | 美国 | 成分分析 | del]
Thanks , I have just been searching for info about this topic for a
while and yours is the greatest I've came upon so far.

However, what in regards to the bottom line? Are you positive about the supply?
回复回复우리카지노[2021-08-26 09:35 PM | IANA | 成分分析 | del]
It is actually a nice and useful piece of info.
I'm glad that you just shared this helpful information with us.
Please stay us up to date like this. Thank you for sharing.
回复回复우리카지노[2021-08-26 03:43 AM | IANA | 成分分析 | del]
Hmm is anyone else encountering problems with the images on this blog loading?
I'm trying to figure out if its a problem on my end
or if it's the blog. Any feedback would be greatly appreciated.
回复回复우리카지노[2021-08-25 00:13 AM | IANA | 成分分析 | del]
Hi everyone, it's my first pay a quick visit at this website,
and piece of writing is really fruitful for me, keep up posting such content.
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: = 9 + 11
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭