可滚动的表格不显眼的脚本

发布于 2024-12-01 16:56:28 字数 1870 浏览 0 评论 0原文

这是一段在所有浏览器中都能正常工作的漂亮代码: http://www.imaputz.com/ cssStuff/bigFourVersion.html

由于需要为每个特定表格计算一些内容(滚动条的额外空间,每个单元格的固定宽度),我想创建一个 javascript 来修改表格以使其可滚动。

此类脚本已经存在,即 http://www.farinspace.com/jquery-scrollable-表插件/ 但它们都需要指定桌子的高度。我想从 CSS 中提取高度以使脚本不引人注目。

如果我将高度写为内联样式,则所有浏览器都可以读取 table.style.height 属性。但是,当使用外部样式时,

<style>
  .scrollTable { display: block; overflow: hidden; height: 200px; }
</style>

只有 Firefox 可以按预期读取 offsetHeight/clientHeight 属性。我猜其他浏览器无法将表格布局更改为阻止。

我唯一想到的就是读取外部 CSS

function getCSS(rule,prop) {
  if(!(document && document.styleSheets)) return;
  var result="", ds = document.styleSheets;
  for(var i=0; i<ds.length; i++) {
    var r = getCSS1(ds[i],rule,prop);
    if(r) result = r;
  }
  return result;
}
function getCSS1(sheet,rule,prop) {
  var rules = sheet.cssRules? sheet.cssRules: sheet.rules;
  var result = "";
  for(var i=0; i<rules.length; i++) {
    var r = rules[i].selectorText.toLowerCase();
    if(r.indexOf(rule)==-1) continue;
    if(r.lastIndexOf(rule)!=r.length-rule.length) continue;
    var p = rules[i].style[prop];
    if(p) result = p;
  }
  return result;
}

var height = getCSS(".scrollTable","height");

但高度可能会以其他方式指定:通过 id、另一个类或继承,所以我可能采用了错误的方式。还有其他方法可以猜测桌子的高度吗?我快要放弃了。


简而言之

我想要这段代码

<script src="scrollTable.js"></script> // load the script
<style> .scrollTable { height: 200px; } // set the CSS
<table class="scrollTable">...</table> // script should do all the tricks

我的脚本可以完成除了获取表格高度之外的所有技巧。如何提取呢?

Here is nice piece of code that works fair in all browsers: http://www.imaputz.com/cssStuff/bigFourVersion.html

Since there are things that need to be computed for every particular table (extra space for scrollbar, fixed width for every cell), I'd like to create a javascript that modifies the table to make it scrollable.

Such scripts already exist, i.e. http://www.farinspace.com/jquery-scrollable-table-plugin/
but all of them need to specify the height of the table. I'd like to extract the height from CSS to make the script unobtrusive.

If I write the height as inline style, all browsers can read the table.style.height property. But when external style is used

<style>
  .scrollTable { display: block; overflow: hidden; height: 200px; }
</style>

only Firefox can read the offsetHeight/clientHeight property as expected. Other browsers can't change table layout to block, I guess.

The only thing that I figured out is to read external CSS

function getCSS(rule,prop) {
  if(!(document && document.styleSheets)) return;
  var result="", ds = document.styleSheets;
  for(var i=0; i<ds.length; i++) {
    var r = getCSS1(ds[i],rule,prop);
    if(r) result = r;
  }
  return result;
}
function getCSS1(sheet,rule,prop) {
  var rules = sheet.cssRules? sheet.cssRules: sheet.rules;
  var result = "";
  for(var i=0; i<rules.length; i++) {
    var r = rules[i].selectorText.toLowerCase();
    if(r.indexOf(rule)==-1) continue;
    if(r.lastIndexOf(rule)!=r.length-rule.length) continue;
    var p = rules[i].style[prop];
    if(p) result = p;
  }
  return result;
}

var height = getCSS(".scrollTable","height");

But the height might be specified otherwise: by id, another class or inherited, so I am probably in wrong way. Is there any other way how to guess the table height? I am about to give up.


In short

I want this code

<script src="scrollTable.js"></script> // load the script
<style> .scrollTable { height: 200px; } // set the CSS
<table class="scrollTable">...</table> // script should do all the tricks

My script can do all the tricks except for get the table height. How to extract it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青瓷清茶倾城歌 2024-12-08 16:56:28

为什么不将 包含在

中?这样,即使它位于外部 CSS 中,您也应该能够获取

高度。

Why not enclose the <table> in a <div>? This way you should be able to the get the <div> height even if it's in an external CSS.

从来不烧饼 2024-12-08 16:56:28

通过 jQuery,您可以使用 .css() 函数:

table_height = $("table.scrollTable").css("height");

这将返回表格的高度,包括 px。因此,您可以使用其他一些 JavaScript 函数仅提取数字。

With jQuery you can use .css() function:

table_height = $("table.scrollTable").css("height");

This will return the height of your table including px. So you can extract only the number with some other javascript function.

彼岸花似海 2024-12-08 16:56:28

假设您正在使用 jQuery(您确实提到了 jQuery 插件),您可以使用 $('table.selector').height() 计算元素的高度。

Assuming you are using jQuery (you did mention a jQuery plugin), you can calculate the height of an element using $('table.selector').height().

第七度阳光i 2024-12-08 16:56:28

getCompatedStyle 是答案吗?请参阅此处:http://blog.stchur.com/2006/06 /21/css-compulated-style/

Is getComputedStyle the answer ? See here : http://blog.stchur.com/2006/06/21/css-computed-style/

韬韬不绝 2024-12-08 16:56:28

你可以使用

document.getElementById("ID").clientHeight

设置的 height 时,它在 IE8 和 Chrome 中对我有用

  • 当我测试通过html
  • 内联
  • 样式表

这就是我所做的:

<table height="350" id="one">
<table> 

<table style="height:400px;" id="two">
<table> 

<table class="height" id="three">
<table> 

alert(document.getElementById("one").clientHeight);

alert(document.getElementById("two").clientHeight);

alert(document.getElementById("three").clientHeight);

工作示例: http://jsfiddle.net/jasongennaro/h9B2j/

编辑

我认为一定有其他东西干扰了代码,如

document.getElementById( "").clientHeight;

即使没有设置高度也适用于我。

请参阅此处的另一个示例http://jsfiddle.net/jasongennaro/h9B2j/ 2/

You could use

document.getElementById("ID").clientHeight

It worked for me in IE8 and Chrome when I tested height set via

  • html
  • inline style
  • style sheet

Here's what I did:

<table height="350" id="one">
<table> 

<table style="height:400px;" id="two">
<table> 

<table class="height" id="three">
<table> 

and

alert(document.getElementById("one").clientHeight);

alert(document.getElementById("two").clientHeight);

alert(document.getElementById("three").clientHeight);

Working example: http://jsfiddle.net/jasongennaro/h9B2j/

EDIT

Something else must be interfering with the code, I think, as

document.getElementById("").clientHeight;

works for me even without the height being set.

See another example here: http://jsfiddle.net/jasongennaro/h9B2j/2/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文