jQuery背景颜色单独更改功能
jQuery background color change on separate functions
剥离工作jsfield
如果行复选框被选中,则行将被高亮显示。它可以在一张桌子上工作,但出于某种原因,我不明白为什么它不能在另一张桌子上做同样的事情。所有其他功能都是我想要的。
$(document).ready(function() {
$('#myteam').on('change', '[type=checkbox]', function() {
var $this = $(this);
var row = $this.closest('tr');
if ($this.prop('checked')) { // move to top
row.insertBefore(row.parent().find('tr:first-child'))
} else { // move to bottom
row.insertAfter(row.parent().find('tr:last-child'))
}
});
$('#otherteam').on('change', '[type=checkbox]', function() {
var $this = $(this);
var row = $this.closest('tr');
if ($this.prop('checked')) { // move to top
row.insertBefore(row.parent().find('tr:first-child'))
} else { // move to bottom
row.insertAfter(row.parent().find('tr:last-child'))
}
});
$('td:first-child input').change(function() {
$(this).closest('#myteam tr').toggleClass("highlight", this.checked);
});
$('td:first-child input').change(function() {
$(this).closest('#otherteam tr').toggleClass("highlight", this.checked);
});
});
As K。Angel7提到,jQuery选择器有一个小问题。
我修复了jsfiddle,现在开始:http://jsfiddle.net/Manoj85/k1xfehrq/98/
$('#otherteam td input').change(function() {
console.log("Other Team");
$(this).closest('#otherteam tr').toggleClass("highlight", this.checked);
});
$('#myteam td input').change(function() {
console.log("My Team");
$(this).closest('#myteam tr').toggleClass("highlight", this.checked);
});
总是要考虑添加特定的父级,在你的例子中是#myteam和#otherteam。这将更好地维护任何更新在未来。
希望能有所帮助。
一个解决方案是使你的选择器更精确。
$('#myteam td:first-child input').change(function() {
$(this).closest('#myteam tr').toggleClass("highlight", this.checked);
});
$('#otherteam td:first-child input').change(function() {
$(this).closest('#otherteam tr').toggleClass("highlight", this.checked);
});
但是,将突出显示开关移动到其他更改功能中会更简单。
$('#myteam').on('change', '[type=checkbox]', function() {
var $this = $(this);
var row = $this.closest('tr');
if ($this.prop('checked')) { // move to top
row.insertBefore(row.parent().find('tr:first-child'))
} else { // move to bottom
row.insertAfter(row.parent().find('tr:last-child'))
}
$this.toggleClass("highlight", this.checked);
});
$('#otherteam').on('change', '[type=checkbox]', function() {
var $this = $(this);
var row = $this.closest('tr');
if ($this.prop('checked')) { // move to top
row.insertBefore(row.parent().find('tr:first-child'))
} else { // move to bottom
row.insertAfter(row.parent().find('tr:last-child'))
}
$this.toggleClass("highlight", this.checked);
});
这应该能回答这个问题。但是,我注意到这些函数可以简化。您可以用以下几行替换所有发布的代码:
$(document).ready(function() {
$('#myteam, #otherteam').find(':checkbox').change(function() {
var $this = $(this);
var row = $this.closest('tr');
if (this.checked) { // move to top
row.prependTo(row.parent())
} else { // move to bottom
row.appendTo(row.parent())
}
row.toggleClass("highlight", this.checked);
});
});
注意,:checkbox
是一个jquery定义的伪选择器
我在这里重构了一些东西
这是$('td:first-child input')
的问题,在第二个更改事件中删除:first-child
将是您所需要的,因为它不是第一个孩子。
相关文章:
- Javascript非常简单:'阅读更多''显示较少'应用于Wordpress的功能
- 如何使用CSS或JQuery使整个网页看起来更小(就像Chrome的缩小功能一样)
- 如何预加载这些图像以使此功能更平滑
- javaScript代码在一个单独的文件中工作,但不是更大文件的一部分
- 如何复制YouTube;s”;显示更多“;功能
- jquery加载了更多的功能
- 向骨干模型添加更多功能
- 运行多事件功能的更简洁的方式
- 谷歌应用脚本 - 将一些功能移动到单独的文件中
- Javascript - 更快的功能:线性列表搜索或获取对象值
- 更好的性能?200+ 个单独的结果页面或 200+ 个条件(如果还有其他梯形图或开关大小写?)在 Javascript/
- 我的加载更多功能适用于按钮单击,但它不适用于滚动
- 如何为每个标签单独运行 Chrome 扩展程序功能
- 我在循环中遇到闭包问题,但我仍然使用单独的功能
- 将 html 内容分解为单独的更高级别 html 标记并将它们存储在数组中
- 复选框选中功能,用于单个页面中的两组复选框,不能单独调用功能
- jQuery单击功能可从元素中删除属性并在单独单击时恢复属性
- 使用 PHP 整理多个 JavaScript 文件将比单独包含它们更快
- 不可变的JS:使函数更多.功能的
- 一次点击即可实现更多功能的问题