j查询切换 - 防止顺序切换
jquery toggle - prevent sequential toggling
>我有一个带有类 agg-drop 的列表,其项目有一个类名 sortedli。这些项目有 2 种切换状态。我使用 .clone(true) 克隆这些项目,它将该项目添加到类名为"all-drop"的列表中。此列表中的项目有 3 种切换状态。现在,如果我要切换新克隆列表中的项目,则前两次单击不执行任何操作,大概是因为它按顺序执行切换功能,并且切换依赖于类名。有没有办法防止这种情况?
$(".sortedli").toggle(function(){
if( $(this).parent().hasClass('agg-drop') ){
$(this).css("background","orange");
}},
function(){
if( $(this).parent().hasClass('agg-drop') ){
$(this).css("background","yellow");
}},
function(){
if( $(this).parent().hasClass('all-drop') ){
$(this).css("background","red");
}},
function(){
if( $(this).parent().hasClass('all-drop') ){
$(this).css("background","green");
}},
function(){
if( $(this).parent().hasClass('all-drop') ){
$(this).css("background","blue");
}}
);
这行得通吗?
$(".sortedli").toggle(function(){
if( $(this).parent().hasClass('agg-drop') ){
$(this).css("background","orange");
}},
function(){
if( $(this).parent().hasClass('agg-drop') ){
$(this).css("background","yellow");
}
}).toggle(function(){
function(){
if( $(this).parent().hasClass('all-drop') ){
$(this).css("background","red");
}},
function(){
if( $(this).parent().hasClass('all-drop') ){
$(this).css("background","green");
}},
function(){
if( $(this).parent().hasClass('all-drop') ){
$(this).css("background","blue");
}
});
您的假设是正确的,即它按顺序通过切换列表。 这也是为什么原始li
更改两次,然后什么都不做 3 次的原因。 您要做的是在克隆ul
后附加一个单独的切换事件
这就是我想象你的代码目前的样子:(http://jsfiddle.net/QGxRK/2/)
$(function() {
$(".sortedli").toggle(function(){
if( $(this).parent().hasClass('agg-drop') ){
$(this).css("background","orange");
}},
function(){
if( $(this).parent().hasClass('agg-drop') ){
$(this).css("background","yellow");
}},
function(){
if( $(this).parent().hasClass('all-drop') ){
$(this).css("background","red");
}},
function(){
if( $(this).parent().hasClass('all-drop') ){
$(this).css("background","green");
}},
function(){
if( $(this).parent().hasClass('all-drop') ){
$(this).css("background","blue");
}}
);
var allDrop = $('.agg-drop').clone(true);
allDrop.removeClass('agg-drop').addClass('all-drop');
allDrop.insertAfter('.agg-drop');
});
这更多是你想要的:http://jsfiddle.net/QGxRK/3/
$(function() {
$(".sortedli").toggle(function(){
if( $(this).parent().hasClass('agg-drop') ){
$(this).css("background","orange");
}},
function(){
if( $(this).parent().hasClass('agg-drop') ){
$(this).css("background","yellow");
}}
);
var allDrop = $('.agg-drop').clone(); //Don't need to clone the events
allDrop.removeClass('agg-drop').addClass('all-drop');
allDrop.insertAfter('.agg-drop');
allDrop.find('.sortedli').toggle(function(){
if( $(this).parent().hasClass('all-drop') ){
$(this).css("background","red");
}
},
function(){
if( $(this).parent().hasClass('all-drop') ){
$(this).css("background","green");
}
},
function(){
if( $(this).parent().hasClass('all-drop') ){
$(this).css("background","blue");
}
});
});
相关文章:
- 内部分区字体大小获胜'调整浏览器窗口大小时不会随媒体查询而更改
- 如何使用JavaScript控制数据库查询的响应顺序
- 是否可以按顺序基于关联进行查询
- j查询切换 - 防止顺序切换
- 节点.js MYSQL,在前一个查询完成后按顺序运行每个查询
- j查询图像预加载顺序
- 全屏期间的媒体查询与伪类 - CSS 样式顺序
- 以正确的顺序触发查询/JavaScript函数
- node.js 和 express :顺序执行流一个接一个的 mongodb 查询请求
- 按顺序发送多个 Ajax 查询在 chrome 和 Safari 中失败
- 在Knockoutjs中使用引用过滤器的CloudKit JS顺序查询
- Sequelize——如何按顺序执行查询
- 在JQuery的每个循环中,Ajax调用MongoDB查询会影响结果的顺序
- 数据源查询回调问题(调用顺序,改变全局变量的能力)
- YQL不按照文档顺序返回带有联合操作符(a|b)的xpath选择查询
- 然后,Javascript承诺在for循环中不按顺序执行Parse查询
- 在NodeJS中处理多个、顺序的、依赖的mongo查询结果的最佳方式
- j查询页面加载顺序加载元素
- Sequelize -使用multipleID而不是顺序进行查询
- Wordpress自定义查询-顺序标题将无法工作