Rest.next()?我的点击功能添加'.活动'两次
Rest .next()? My click function adding '.active' twice?
如果你看看这个小提琴,它看起来会很好,但如果你点击下一个,向下移动2到3次,然后点击"存储器";(在顶部导航中)它将.active
带回第一个.item
,
然后,如果您再次单击"下一步",它将继续转到我们中断的上一个元素的下一个元素。
我正在尝试重置它,并在点击顶部导航后根据我们的位置继续。
错误的jQuery:*两个点击功能都添加了活动*
var items = $('.item'),
currentItem = items.filter('.active'),
last = items.last();
$("#next-button").on('click', function () {
currentItem.removeClass('active');
var nextItem = currentItem.next();
if (nextItem.length) {
currentItem = nextItem.addClass('active');
if (currentItem.is(last)) {
$('#slide-buttons').addClass('red');
}
}
var items = $('.item');
$(".breadcrumb-cell .breadcrumb").click(function () {
var theID = $(this).data("id");
items.filter(function() {
return $(this).data('category') === theID;
}).addClass('active');
});
});
Fiddle
我在谷歌上搜索了";如何重置.next()jquery;但什么都找不到,不确定这样做是否正确?
您遇到的问题是,当您单击面包屑时,currentItem
没有得到更新。
我做了很多改变,主要是"精简"。我删除了您的全局变量,并将当前项改为基于active
类。检查:http://jsfiddle.net/kQabJ/17/
$("#next-button").on('click', function () {
var nextItem = $('.active').removeClass('active').next();
if (!nextItem.length) {
nextItem = $('.item').first();
}
nextItem.addClass('active');
});
$(".breadcrumb-cell .breadcrumb").on('click', function () {
$('.active').removeClass('active');
var theID = $(this).data("id");
$("#" + theID).addClass('active');
});
请注意,我还修改了您的DOM,以便在用户单击面包屑时更容易地选择项目。这种变化是在.item
上使用ID而不是数据。通过这种方式,您可以执行$("#" + theID)
,而不是基于数据进行筛选。
由于这些东西是唯一标识.item
元素本身的,因此无论如何都可以使用id,但如果不是这样,则可以随时更改该部分。
您只需要更新currentItem
,请参阅http://jsfiddle.net/kQabJ/13/
$(".breadcrumb-cell .breadcrumb").on('click', function () {
items.removeClass('active');
var theID = $(this).data("id");
items.filter(function() {
return $(this).data('category') === theID;
}).addClass('active');
currentItem = items.filter('.active');
});
试试这个代码您没有更新导致问题的currentItem
。
var items = $('.item'),
currentItem = items.filter('.active'),
last = items.last();
$("#next-button").on('click', function () {
currentItem = items.filter('.active');
var nextItem = currentItem.next();
currentItem.next().length > 0 ? currentItem.next().addClass('active')
: items.first().addClass('active');
currentItem.removeClass('active');
});
$(".breadcrumb-cell .breadcrumb").on('click', function () {
items.removeClass('active');
var theID = $(this).data("id");
items.filter(function () {
return $(this).data('category') === theID;
}).addClass('active');
});
检查Fiddle
相关文章:
- 主干.js绑定到集合“添加”呈现视图两次
- 元素必须单击两次才能添加Class(自定义指令)
- jQuery add - 到输入,但只添加两次
- 以嵌套形式动态生成的字段添加了两次 Rails
- rails link_to_add_fields 以嵌套形式将每个字段添加两次
- 非重复数组编号(使用 Jquery 添加两次或更多
- 向同一按钮添加两次单击事件只能工作一次
- Rest.next()?我的点击功能添加'.活动'两次
- 在一个元素上添加两次相同的类
- ascx控件两次添加到页面时调用了错误的javascript函数
- Jquery库在一个页面中添加两次时出现问题
- Javascript appendChild将一个元素添加到DOM中两次
- 一次向一个元素添加两个元素,一个在另一个内部
- 如何添加值到javascript数组两次或更多
- 当点击点赞按钮并在点赞中添加评论时,IE 9在脸书墙上发布了两次
- Javascript moments .js添加日期(每月两次)
- 在Jquery中添加两次图像
- 重定向时添加了两次参数
- 不能将同一个模型添加到一个集合中两次
- Javascript添加两次并检查条件