使用 Javascript 隐藏/显示表列(修复所有浏览器?)

发布于 2024-11-06 23:27:54 字数 745 浏览 0 评论 0原文

我有一个长表,有很多列,对于用户来说它看起来真的很难看。我想做的是创建一个简单的按钮,充​​当开关,打开和关闭某些列。

有些列是不需要的,所以我所做的就是为每个不需要的列添加一个类,例如: ....

现在,我认为我可以做的是:

var hidden = 1;
     function toggleTable(){
      element_array = document.getElementsByClassName('disabled');
      for(i = 0; i < element_array.length; i++){
      if(hidden == 1){
        element_array[i].style.display = 'none';
      }else{
        element_array[i].style.display = '';
      }
      }

      if(hidden == 1) hidden = 0;
      else hidden = 1;
     }

这在 Firefox 中大部分有效,但在 IE(7+8) 中进行一些快速测试,我得到以下结果: 消息:对象不支持此属性或方法

显然表明 IE 不想让我简单地更改“display:none;”对于诸如表列/行之类的内容。

我想不出任何解决方法。理想情况下,我想要一个完全交叉兼容的解决方案来切换某些表列的显示,但如果它在旧版浏览器(例如:IE6)中不兼容,那么也可以。

I have a long table with many many columns and it looks really ugly for the users. What I wanted to do was create a simple button that would act as a switch, to turn on and off some of the columns.

Some of the columns are not needed, so what I did was add a class to every that wasn't needed, eg: ....

Now, what I thought I could do was this:

var hidden = 1;
     function toggleTable(){
      element_array = document.getElementsByClassName('disabled');
      for(i = 0; i < element_array.length; i++){
      if(hidden == 1){
        element_array[i].style.display = 'none';
      }else{
        element_array[i].style.display = '';
      }
      }

      if(hidden == 1) hidden = 0;
      else hidden = 1;
     }

This works for the most part in Firefox, but some quick tests in IE(7+8) and I get the following:
Message: Object doesn't support this property or method

Obviously indicating that IE doesn't want to let me simply change "display: none;" for something like table columns/rows.

I can't think of any workarounds. Ideally I'd like a fully cross-compatible solution to toggling the display of certain table columns,but if it's not compatible in the older browsers (eg: IE6) then that would also be OK.

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

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

发布评论

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

评论(2

初心未许 2024-11-13 23:27:54

您收到的错误不是因为 IE 不想设置显示属性,而是因为 IE 中未实现 getElementsByClassName 方法。如果您想要该方法的实现,您可以使用达斯汀·迪亚兹 (Dustin Diaz) 编写的这个方法。

function getElementsByClass(searchClass,node,tag) {
    var classElements = new Array();
    if ( node == null )
        node = document;
    if ( tag == null )
        tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
        if ( pattern.test(els[i].className) ) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}

然后您将按如下方式重写您的方法。

var hidden = 1;

function toggleTable(){
    var element_array = getElementsByClass('foo');
    for(i = 0; i < element_array.length; i++){
        if(hidden == 1){
            element_array[i].style.display = 'none';
        }else{
            element_array[i].style.display = '';
        }
    }

    if(hidden == 1) hidden = 0;
    else hidden = 1;
}
toggleTable();

The error that you're getting is not because IE doesn't want to set the display property, it's because the getElementsByClassName method isn't implemented in IE. If you want an implementation of that methods you can use this one which was written by Dustin Diaz.

function getElementsByClass(searchClass,node,tag) {
    var classElements = new Array();
    if ( node == null )
        node = document;
    if ( tag == null )
        tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
        if ( pattern.test(els[i].className) ) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}

Then you would re-write your method as follows.

var hidden = 1;

function toggleTable(){
    var element_array = getElementsByClass('foo');
    for(i = 0; i < element_array.length; i++){
        if(hidden == 1){
            element_array[i].style.display = 'none';
        }else{
            element_array[i].style.display = '';
        }
    }

    if(hidden == 1) hidden = 0;
    else hidden = 1;
}
toggleTable();
停滞 2024-11-13 23:27:54

那么 jQuery.toggle() 又如何呢?

$(".disabled").toggle();

And what about jQuery.toggle()?

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