gpt4 book ai didi

javascript - Colspan 属性未从变量设置

转载 作者:行者123 更新时间:2023-11-30 21:12:51 27 4
gpt4 key购买 nike

我有一个表格,其中的单元格具有相同的值。我正在尝试使用 colspan 将它们合并在一起,但是为 colspan 设置属性没有任何效果。

https://jsfiddle.net/afn4f1q9/4/

$(function() {

$('table.merge tr').each(function() {
var tr = this;
var counter = 1;
var prevValue = false;
var colspanTD = false;

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

if (counter == 1) {
colspanTD = $(this);
}

if (prevValue !== false) {
if (td.text() == prevValue) {

if (td.text() != '' && td.text() != 'X' && td.text() != '*') {
counter++;
td.remove();

}
} else {
$(colspanTD).attr('colspan', counter );
counter = 1;
}

}
prevValue = td.text();
});

});



});

最佳答案

I am trying to merge them together using colspan, but setting the attribute for colspan is having no effect.

您的问题与代码的某些问题有关。我解决了问题。这是我的建议(请参阅代码中的注释)。

$('table.merge tr').each(function () {
var tr = this;
var counter = -1;
var prevValue = false;
var colspanTD = false;
//
// save current selection
//
var tableCells = $('td', tr)

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

if (prevValue !== false) {
if (td.text() == prevValue) {
//
// get text, trim it and transform in uppercase
//
var txt = td.text().trim().toUpperCase();
if (txt != '' && txt != 'X' && txt != '*') {
//
// set the starting cell sequence...
//
if (colspanTD === false) {
counter = 1;
colspanTD = tableCells.eq(index - 1);
}
counter++;
td.remove();
}
} else {
//
// if ending cell sequence...
//
if (colspanTD !== false) {
colspanTD.attr('colspan', counter);
colspanTD = false;
}
}
}
prevValue = td.text();
});
//
// if ending cell sequence in row...
//
if (colspanTD !== false) {
colspanTD.attr('colspan', counter);
colspanTD = false;
}

});
th,
td {
border: 1px solid #ccc
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<table class="merge table table-sm">
<thead class="thead-default">
<tr>
<th>Pitch</th>
<th>Identifier</th>
<th data-date="2017-08-01" data-roomid="22">1</th>
<th data-date="2017-08-02" data-roomid="22">2</th>
<th data-date="2017-08-03" data-roomid="22">3</th>
<th data-date="2017-08-04" data-roomid="22">4</th>
<th data-date="2017-08-05" data-roomid="22">5</th>
<th data-date="2017-08-06" data-roomid="22">6</th>
<th data-date="2017-08-07" data-roomid="22">7</th>
<th data-date="2017-08-08" data-roomid="22">8</th>
<th data-date="2017-08-09" data-roomid="22">9</th>
<th data-date="2017-08-10" data-roomid="22">10</th>
<th data-date="2017-08-11" data-roomid="22">11</th>
<th data-date="2017-08-12" data-roomid="22">12</th>
<th data-date="2017-08-13" data-roomid="22">13</th>
<th data-date="2017-08-14" data-roomid="22">14</th>
<th data-date="2017-08-15" data-roomid="22">15</th>
<th data-date="2017-08-16" data-roomid="22">16</th>
<th data-date="2017-08-17" data-roomid="22">17</th>
<th data-date="2017-08-18" data-roomid="22">18</th>
<th data-date="2017-08-19" data-roomid="22">19</th>
<th data-date="2017-08-20" data-roomid="22">20</th>
<th data-date="2017-08-21" data-roomid="22">21</th>
<th data-date="2017-08-22" data-roomid="22">22</th>
<th data-date="2017-08-23" data-roomid="22">23</th>
<th data-date="2017-08-24" data-roomid="22">24</th>
<th data-date="2017-08-25" data-roomid="22">25</th>
<th data-date="2017-08-26" data-roomid="22">26</th>
<th data-date="2017-08-27" data-roomid="22">27</th>
<th data-date="2017-08-28" data-roomid="22">28</th>
<th data-date="2017-08-29" data-roomid="22">29</th>
<th class="availability-box-header" data-date="2017-08-30" data-roomid="22">30</th>
<th class="availability-box-header" data-date="2017-08-31" data-roomid="22">31</th>
</tr>
</thead>
<tbody>
<tr>
<td>Glade - Electric</td>
<td>G1</td>
<td class="text-center">
<a href="#">771</a>
</td>
<td class="text-center">
<a href="#">771</a>
</td>
<td class="text-center">
<a href="#">771</a>
</td>
<td class="text-center">
<a href="#">771</a>
</td>
<td class="availability-box text-center text-danger" data-roomid="22" data-identifier="70"
data-date="2017-08-05" data-block="false">X
</td>
<td class="availability-box text-center text-danger" data-roomid="22" data-identifier="70"
data-date="2017-08-06" data-block="false">X
</td>
<td class="availability-box text-center text-danger" data-roomid="22" data-identifier="70"
data-date="2017-08-07" data-block="false">X
</td>
<td class="availability-box text-center text-danger" data-roomid="22" data-identifier="70"
data-date="2017-08-08" data-block="false">X
</td>
<td class="text-center">
<a href="#">747</a>
</td>
<td class="text-center">
<a href="#">747</a>
</td>
<td class="text-center">
<a href="#">747</a>
</td>
<td class="text-center">
<a href="#">747</a>
</td>
<td class="text-center">
<a href="#">747</a>
</td>
<td class="text-center">
<a href="#">802</a>
</td>
<td class="text-center">
<a href="#">802</a>
</td>
<td class="text-center">
<a href="#">802</a>
</td>
<td class="text-center">
<a href="#">802</a>
</td>
<td class="text-center">
<a href="#">802</a>
</td>
<td class="text-center">
<a href="#">802</a>
</td>
<td class="text-center">
<a href="#">1276</a>
</td>
<td class="text-center">
<a href="#">1276</a>
</td>
<td class="text-center">
<a href="#">1276</a>
</td>
<td class="text-center">
<a href="#">1276</a>
</td>
<td class="text-center">
<a href="#">1276</a>
</td>
<td class="text-center">
<a href="#">1276</a>
</td>
<td class="text-center">
<a href="#">1286</a>
</td>
<td class="text-center">
<a href="#">1286</a>
</td>
<td class="text-center text-danger" data-roomid="22" data-identifier="70" data-date="2017-08-28"
data-block="true"></td>
<td class="text-center text-danger" data-roomid="22" data-identifier="70" data-date="2017-08-29"
data-block="true"></td>
<td class="text-center">
<a href="#">1194</a>
</td>
<td class="text-center">
<a href="#">1194</a>
</td>
</tr>
</tbody>

</table>
<br/><br/>

<table class="table table-sm">
<thead class="thead-default">
<tr>
<th>Pitch</th>
<th>Identifier</th>
<th data-date="2017-08-01" data-roomid="22">1</th>
<th data-date="2017-08-02" data-roomid="22">2</th>
<th data-date="2017-08-03" data-roomid="22">3</th>
<th data-date="2017-08-04" data-roomid="22">4</th>
<th data-date="2017-08-05" data-roomid="22">5</th>
<th data-date="2017-08-06" data-roomid="22">6</th>
<th data-date="2017-08-07" data-roomid="22">7</th>
<th data-date="2017-08-08" data-roomid="22">8</th>
<th data-date="2017-08-09" data-roomid="22">9</th>
<th data-date="2017-08-10" data-roomid="22">10</th>
<th data-date="2017-08-11" data-roomid="22">11</th>
<th data-date="2017-08-12" data-roomid="22">12</th>
<th data-date="2017-08-13" data-roomid="22">13</th>
<th data-date="2017-08-14" data-roomid="22">14</th>
<th data-date="2017-08-15" data-roomid="22">15</th>
<th data-date="2017-08-16" data-roomid="22">16</th>
<th data-date="2017-08-17" data-roomid="22">17</th>
<th data-date="2017-08-18" data-roomid="22">18</th>
<th data-date="2017-08-19" data-roomid="22">19</th>
<th data-date="2017-08-20" data-roomid="22">20</th>
<th data-date="2017-08-21" data-roomid="22">21</th>
<th data-date="2017-08-22" data-roomid="22">22</th>
<th data-date="2017-08-23" data-roomid="22">23</th>
<th data-date="2017-08-24" data-roomid="22">24</th>
<th data-date="2017-08-25" data-roomid="22">25</th>
<th data-date="2017-08-26" data-roomid="22">26</th>
<th data-date="2017-08-27" data-roomid="22">27</th>
<th data-date="2017-08-28" data-roomid="22">28</th>
<th data-date="2017-08-29" data-roomid="22">29</th>
<th class="availability-box-header" data-date="2017-08-30" data-roomid="22">30</th>
<th class="availability-box-header" data-date="2017-08-31" data-roomid="22">31</th>
</tr>
</thead>
<tbody>
<tr>
<td>Glade - Electric</td>
<td>G1</td>
<td class="text-center">
<a href="#">771</a>
</td>
<td class="text-center">
<a href="#">771</a>
</td>
<td class="text-center">
<a href="#">771</a>
</td>
<td class="text-center">
<a href="#">771</a>
</td>
<td class="availability-box text-center text-danger" data-roomid="22" data-identifier="70"
data-date="2017-08-05" data-block="false">X
</td>
<td class="availability-box text-center text-danger" data-roomid="22" data-identifier="70"
data-date="2017-08-06" data-block="false">X
</td>
<td class="availability-box text-center text-danger" data-roomid="22" data-identifier="70"
data-date="2017-08-07" data-block="false">X
</td>
<td class="availability-box text-center text-danger" data-roomid="22" data-identifier="70"
data-date="2017-08-08" data-block="false">X
</td>
<td class="text-center">
<a href="#">747</a>
</td>
<td class="text-center">
<a href="#">747</a>
</td>
<td class="text-center">
<a href="#">747</a>
</td>
<td class="text-center">
<a href="#">747</a>
</td>
<td class="text-center">
<a href="#">747</a>
</td>
<td class="text-center">
<a href="#">802</a>
</td>
<td class="text-center">
<a href="#">802</a>
</td>
<td class="text-center">
<a href="#">802</a>
</td>
<td class="text-center">
<a href="#">802</a>
</td>
<td class="text-center">
<a href="#">802</a>
</td>
<td class="text-center">
<a href="#">802</a>
</td>
<td class="text-center">
<a href="#">1276</a>
</td>
<td class="text-center">
<a href="#">1276</a>
</td>
<td class="text-center">
<a href="#">1276</a>
</td>
<td class="text-center">
<a href="#">1276</a>
</td>
<td class="text-center">
<a href="#">1276</a>
</td>
<td class="text-center">
<a href="#">1276</a>
</td>
<td class="text-center">
<a href="#">1286</a>
</td>
<td class="text-center">
<a href="#">1286</a>
</td>
<td class="text-center text-danger" data-roomid="22" data-identifier="70" data-date="2017-08-28"
data-block="true"></td>
<td class="text-center text-danger" data-roomid="22" data-identifier="70" data-date="2017-08-29"
data-block="true"></td>
<td class="text-center">
<a href="#">1194</a>
</td>
<td class="text-center">
<a href="#">1194</a>
</td>
</tr>
</tbody>

</table>

关于javascript - Colspan 属性未从变量设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45961615/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com