使用 jQuery 动态设置 colspan [英] Set colspan dynamically with jQuery

查看:21
本文介绍了使用 jQuery 动态设置 colspan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的简单表结构.我想做的是根据 <td> 中的某些条件动态合并一些列,例如,如果 td1 和 td3 为空,则合并单元格并执行1Meeting我尝试使用 jQuery:

I have a simple table structure like this. What I would like to do is to dynamically merge some columns based on some condition within the <td> for example, if td1 and td3 are empty then merge the cells and do <td class="col1" colspan="3">1Meeting</td> I tried playing around with jQuery using:

 $(".tblSimpleAgenda  td:contains('')").hide();

但是没有效果.

使用 jQuery 实现这一目标的最佳方法是什么.

What would be the the best way using jQuery to achieve this.

<table  class="tblSimpleAgenda" cellpadding="5" cellspacing="0">
 <tbody>
 <th align="left">Time</th>
 <th align="left">Room 1</th>
 <th align="left">Room 2</th>
 <th align="left">Room 3</th> 
    
        <tr valign="top">
            <td class="colTime">09:00 – 10:00</td>
            <td class="col1"></td>
            <td class="col2">Meeting 2</td>
            <td class="col3"></td>
        </tr>
        
        <tr valign="top">
            <td class="colTime">10:00 – 10:45</td>
            <td class="col1">Meeting 1</td>
            <td class="col2">Meeting 2</td>
            <td class="col3">Meeting 3</td> 
        </tr>
        
        <tr valign="top">
            <td class="colTime">11:00 – 11:45</td>
            <td class="col1">Meeting 1</td>
            <td class="col2">Meeting 2</td>
            <td class="col3">Meeting 3</td> 
        </tr>
</tbody>
</table> 

推荐答案

怎么样

$([your selector]).attr('colspan',3);

我认为这可行,但目前无法进行测试.使用 .attr() 将是设置属性的常用 jQuery 方式包装集中的元素.

I would imagine that to work but have no way to test at the moment. Using .attr() would be the usual jQuery way of setting attributes of elements in the wrapped set.

正如在另一个答案中提到的那样,为了使其工作,需要从 DOM 中删除没有文本的 td 元素.在所有服务器端执行此操作可能更容易

As has been mentioned in another answer, in order to get this to work would require removing the td elements that have no text in them from the DOM. It may be easier to do this all server side

正如评论中提到的,在 IE 中尝试使用 attr() 设置 colspan 存在错误,但以下在 IE6 和 FireFox 3.0.13 中有效.

As was mentioned in the comments, there is a bug in attempting to set colspan using attr() in IE, but the following works in IE6 and FireFox 3.0.13.

工作演示

注意使用属性 colSpan 而不是 colspan - 前者在 IE 和 Firefox 中都有效,但后者在 IE 中不起作用.查看 jQuery 1.3.2 源代码,如果

notice the use of the attribute colSpan and not colspan - the former works in both IE and Firefox, but the latter does not work in IE. Looking at jQuery 1.3.2 source, it would appear that attr() attempts to set the attribute as a property of the element if

  1. 它作为元素上的一个属性存在(colSpan 作为一个属性存在,并且在 IE 和 FireFox 中的 HTMLElements 上默认为 1)
  2. 文档不是 xml 并且
  3. 该属性不是 href、src 或 style
  1. it exists as a property on the element (colSpan exists as a property and defaults to 1 on <td> HTMLElements in IE and FireFox)
  2. the document is not xml and
  3. the attribute is none of href, src or style

使用 colSpan 而不是 colspanattr() 一起使用,因为前者是在元素上定义的属性,而后者不是.

using colSpan as opposed to colspan works with attr() because the former is a property defined on the element whereas the latter is not.

attr() 的失败是尝试在有问题的元素上使用 setAttribute(),将值设置为字符串,但这会导致问题在 IE 中(jQuery 中的错误 #1070)

the fall-through for attr() is to attempt to use setAttribute() on the element in question, setting the value to a string, but this causes problems in IE (bug #1070 in jQuery)

// convert the value to a string (all browsers do this but IE) see #1070
elem.setAttribute( name, "" + value ); 

在演示中,对于每一行,评估每个单元格中的文本.如果文本是空字符串,则删除单元格并递增计数器.行中没有 class="colTime" 的第一个单元格的 colspan 属性设置为计数器的值 + 1(对于它自己占用的跨度).

In the demo, for each row, the text in each cell is evaluated. If the text is a blank string, then the cell is removed and a counter incremented. The first cell in the row that does not have class="colTime" has a colspan attribute set to the value of the counter + 1 (for the span it occupies itself).

此后,对于每一行,class="colspans" 的单元格中的文本从左到右设置为该行中每个单元格的 colspan 属性值.

After this, for each row, the text in the cell with class="colspans" is set to the colspan attribute values of each cell in the row from left to right.

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<title>Sandbox</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style type="text/css" media="screen">
body { background-color: #000; font: 16px Helvetica, Arial; color: #fff; }
td { text-align: center; }
</style>
</head>
<body>
<table  class="tblSimpleAgenda" cellpadding="5" cellspacing="0">
 <tbody>
        <tr>
            <th align="left">Time</th>
            <th align="left">Room 1</th>
            <th align="left">Room 2</th>
            <th align="left">Room 3</th> 
            <th align="left">Colspans (L -> R)</th>
        </tr>
        <tr valign="top">
            <td class="colTime">09:00 – 10:00</td>
            <td class="col1"></td>
            <td class="col2">Meeting 2</td>
            <td class="col3"></td>
            <td class="colspans">holder</td>
        </tr>

        <tr valign="top">
            <td class="colTime">10:00 – 10:45</td>
            <td class="col1">Meeting 1</td>
            <td class="col2">Meeting 2</td>
            <td class="col3">Meeting 3</td>    
             <td class="colspans">holder</td> 
        </tr>

        <tr valign="top">
            <td class="colTime">11:00 – 11:45</td>
            <td class="col1">Meeting 1</td>
            <td class="col2">Meeting 2</td>
            <td class="col3">Meeting 3</td>
            <td class="colspans">holder</td>     
        </tr>

        <tr valign="top">
            <td class="colTime">11:00 – 11:45</td>
            <td class="col1">Meeting 1</td>
            <td class="col2">Meeting 2</td>
            <td class="col3"></td>
            <td class="colspans">holder</td>     
        </tr>
</tbody>
</table>

</body>
</html>

jQuery 代码

$(function() {

  $('table.tblSimpleAgenda tr').each(function() {
    var tr = this;
    var counter = 0;

    $('td', tr).each(function(index, value) {
      var td = $(this);

      if (td.text() == "") {
        counter++;
        td.remove();
      }
    });

    if (counter !== 0) {
      $('td:not(.colTime):first', tr)
        .attr('colSpan', '' + parseInt(counter + 1,10) + '');
    }
  });

  $('td.colspans').each(function(){
    var td = $(this);
    var colspans = [];

    td.siblings().each(function() {
      colspans.push(($(this).attr('colSpan')) == null ? 1 : $(this).attr('colSpan'));
    });

    td.text(colspans.join(','));
  });

});

这只是一个演示,表明可以使用 attr(),但要注意它的实现以及它带来的跨浏览器的怪癖.我还在演示中对您的表格布局做了一些假设(即,将 colspan 应用于每行中的第一个非时间"单元格),但希望您能理解.

This is just a demonstration to show that attr() can be used, but to be aware of it's implementation and the cross-browser quirks that come with it. I've also made some assumptions about your table layout in the demo (i.e. apply the colspan to the first "non-Time" cell in each row), but hopefully you get the idea.

这篇关于使用 jQuery 动态设置 colspan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆