- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个表,其中标题第一行有两行,有 colspan,第二行有第二行标题,请参见下图
我有一个 jquery 函数,它从第二个标题行获取所有表格单元格值,并将其添加到带有复选框的 div 以及稍后用于切换的索引号。 第一列、第二列和第三列转到名为speed、cons、dest的单独 div
现在切换工作正常,它会检查列的索引号并向其添加隐藏内容,相应地,列跨度会减少以匹配表格格式。
但是每当单击第二列和第三列的第一个复选框时,表格就会中断,我试图找出问题所在,但无法使其工作。有什么建议吗?
这是fiddle请建议
下面是其中一个 div 的过滤器切换
$('.dest input[type=checkbox]').click(function() {
var index = $(this).attr('index');
$('.table thead .this_h th').eq(index).toggleClass('hidden');
var hidden = $('.table thead th.hidden')
$.each(hidden, function() {
var idx = $(this).index() + 1;
$.each($('.table tbody tr'), function() {
$(this).find('td').eq(idx).hide();
});
});
var visible = $('.table thead .this_h th:not(.hidden)');
$.each(visible, function() {
var idx = $(this).index() + 1;
$.each($('.table tbody tr'), function() {
$(this).find('td').eq(idx).show();
});
});
var length = 0;
var temp_name = '';
$(".table thead tr:nth-child(2)").find('th').each(function(e, a) {
if (temp_name == $(a).attr('name')) {
console.log($(a).attr('name'));
if ($(a).is(':visible')) {
length = length + 1;
}
}
else
{
console.log(temp_name);
$(".table thead tr:nth-child(1)").find("th[name=" + temp_name + "]").attr('colspan', length);
if ($(a).is(':visible')) {
length = 1;
temp_name = $(a).attr('name');
} else {
length = 0;
}
}
})
});
最佳答案
给你。
var tableArr = new Array();
var flag = 0;
var speed = new Array();
var speed_index = new Array();
var cons = new Array();
var cons_index = new Array();
var dest = new Array();
var dest_index = new Array();
// Gets the values from the table cell and creates the checkboxes
function get_values() {
if (flag == 0) {
flag = 1;
$.each(speed, function(i) {
$(".top-filter .speed").append('<input type=\'checkbox\' index=' + speed_index[i] + '> ' + speed[i] + '<br/>');
});
$.each(cons, function(i, val) {
$(".top-filter .cons").append('<input type=\'checkbox\' index=' + cons_index[i] + '> ' + cons[i] + '<br/>');
});
$.each(dest, function(i, val) {
$(".top-filter .dest").append('<input type=\'checkbox\' index=' + dest_index[i] + '> ' + dest[i] + '<br/>');
});
}
};
$(document).ready(function() {
// to push the values from cell to array
$('.table thead tr:nth-child(2) th').each(function(e, a) {
if ($(a).attr('name') == 'speed') {
speed.push($(this).text());
speed_index.push($(this).index());
} else if ($(a).attr('name') == 'cons') {
cons.push($(this).text());
cons_index.push($(this).index());
} else {
dest.push($(this).text());
dest_index.push($(this).index());
}
});
get_values();
$('input[type=checkbox]').click(function() {
var index = $(this).attr('index');
var name = $(this).parent().attr("class");
$('.table thead .this_h th').eq(index).toggleClass('hidden');
var hidden = $('.table thead th.hidden')
$.each(hidden, function() {
var idx = $(this).index() + 1;
$.each($('.table tbody tr'), function() {
$(this).find('td').eq(idx).hide();
});
});
var visible = $('.table thead .this_h th:not(.hidden)');
$.each(visible, function() {
var idx = $(this).index() + 1;
$.each($('.table tbody tr'), function() {
$(this).find('td').eq(idx).show();
});
});
var length = $(".table thead tr:nth-child(2)").find("th[name=" + name + "]").filter(':visible').length;
if (length === 0) {
$(".table thead tr:nth-child(1)").find("th[name=" + name + "]").addClass("hidden");
} else {
$(".table thead tr:nth-child(1)").find("th[name=" + name + "]").removeClass("hidden").attr('colspan', length);
}
});
});
.hidden {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<table class="table table-condensed table-striped table-bordered">
<thead>
<tr>
<th rowspan="2">Name</th>
<th name="speed" colspan="3">1st Column</th>
<th name="cons" colspan="4">2st Column</th>
<th name="dest" colspan="3">3st Column</th>
</tr>
<tr class="this_h">
<th name="speed">A</th>
<th name="speed">B</th>
<th name="speed">C</th>
<th name="cons">D</th>
<th name="cons">E</th>
<th name="cons">F</th>
<th name="cons">G</th>
<th name="dest">H</th>
<th name="dest">I</th>
<th name="dest">J</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Doe</td>
<td>22</td>
<td>02</td>
<td>05</td>
<td>2</td>
<td>52</td>
<td>47</td>
<td>22</td>
<td>02</td>
<td>05</td>
<td>2</td>
</tr>
</tbody>
</table>
<div class="row top-filter">
<div class="col-xs-3">
<div class="speed">
</div>
</div>
<div class="col-xs-3">
<div class="cons">
</div>
</div>
<div class="col-xs-3">
<div class="dest">
</div>
</div>
</div>
关于javascript - Jquery基于index()和自定义attr问题的列切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35769675/
我只是想知道下面的$(*[attr])和$([attr])中哪一个更可取。为什么? 因为两者都在做同样的事情。 $('[onclick]').each(function(i,elem){ cons
长期以来我一直在尝试找出问题所在,但不幸的是无法 如果我这样做 android.enableAapt2=true 代码工作正常,但是删除相同的(应该是强制性的)会抛出一个错误说 \incrementa
使用此代码: $('#ipadmenu section').attr('data-order', hash) 我将 data-order 属性设置为“hash”的值。如何选择具有该属性/值的元素而不是
我有一个用于扩展的组合框监听器。展开后,它会对组合框选项中具有特定类的每个元素执行一些样式设置。所需的更改之一是根据当前属性的值更改属性。使用 this 返回未定义。 expand : funct
性能上有区别吗 :not([attr="value"]) 和 [attr!="value"] ? CSS3 规范是否推荐了一种替代方案? 编辑: CSS3 规范不包含 [attr!="value"]
所以我有一个叫做 say,mySave 的指令,它几乎就是这个 app.directive('mySave', function($http) { return function(scope,
有人可以告诉我有什么区别吗 ?android:attr/colorPrimary 和 ?attr/colorPrimary 无论我使用哪个,结果都是一样的。尽管第一个选项导致android.view.
Xpath问题: 何时使用@和属性,何时不使用。有关系吗?有什么区别 最佳答案 使用//tag[attr]时,将选择所有具有至少一个名为tag的子元素的attr元素。另一方面,使用//tag[@att
android布局xml文件中的?android:attr/和?attr/有什么区别?在不同的情况下我们应该使用哪一个? 最佳答案 1。 ?attr/ 定义并引用您在应用程序中自行定义的属性的值。 2
如果 obj 不存在 obj? 生成一个 nil 所以 obj?.attr 也是。 如果 obj 为 nil,则 obj!.attr 崩溃。 但是如果我确定 obj 在代码的某个点总是存在,那么对我来
有一个这样的 HTML。 Back 1 2 3 Next 为了获得最大的页数,我写了这篇文章。 doc = Nokogiri::HTML(html) doc.xpath('//
我想知道这些标签在 android xml 中如何工作。例如在造型方面 style="?android:attr/buttonBarButtonStyle" 和 style="@android:att
这是我的代码: $("input[name=donationmode]").change(function() { $(".setpaymentOptions").children().add
我已经搜索了一段时间,但没有找到我正在搜索的内容。 事情是这样的,我有两张表,我们称它们为表 A 和 B。当 A 更新时,我需要更新 B 中的属性。例如:A. 电子邮件和 B. 电子邮件。当用户在 A
大家好,在访问一个循环的 php 变量时遇到了一个小问题。我的脚本循环使用来自 mysql 数据库的 x 和 y。它还循环出我无法访问的 id,它显示为未定义。我正在使用鼠标移开功能来检测已循环的每个
我将自己的标签转换为输入。我可以使用 select 为输入选择只读/禁用。我做到了。有用。但不是在 ie8 )). 我阅读了 Angular 和 IE8 的官方文档。我添加了它。但是我的应用程序在 i
我正在使用令人惊叹的 attrs 库以一种非常优雅的方式定义许多对象属性,到目前为止它一直运行得非常棒。 我目前遇到的唯一问题是,有时我想通过引用其他 attr.ib() 属性来定义默认值。如果 na
我注意到 javascript 有几种方法来设置和获取元素的属性。 我不确定它们之间有什么区别。特别是,是否存在跨浏览器问题。 最佳答案 DOM 元素的特性和属性有很大不同,这种差异是一些混淆的根源。
在 4.x 设备上,使用 ?android:attr/selectableItemBackgroundBorderless 的布局文件会导致崩溃,但 ?attr/selectableItemBackg
.attr('disabled', 'disabled') 和 .attr('disabled', true) 在我的代码中都有效,但我只是想知道:两者中哪一个更有效和/或哪一个更常用?真的有区别吗?
我是一名优秀的程序员,十分优秀!