sách gpt4 ai đã đi

javascript - 获取父级中的 div 数量,然后获取特定类的 div 数量

In lại 作者:行者123 更新时间:2023-12-02 14:16:07 37 4
mua khóa gpt4 Nike

我有几个带有视频和图像的 Bootstrap slider 。在 slider 之外,我想要一个可以转到包含视频的幻灯片的按钮。包含视频的幻灯片的数量因 slider 而异。我想做的是获取幻灯片的数量,然后获取类 video-slide

的幻灯片编号

有没有办法使用 Bootstrap 轮播函数或使用自定义 js 的某种方式来做到这一点?

标记示例


JS 代码,我使用 php 从 WP 获取帖子 ID,为每个帖子创建一个唯一的 slider 。

for(var i = 0; i <= jQuery('.carousel- div.item').length; i++) {
if (jQuery('.carousel- div.item')[i].hasClass('video-slide')) {
//i is the position of the video slide
console.log(i);
}
}

根据 RhysO 的 JS 输出

for(var i4689 = 0; i4689 <= $('.carousel-4689 div.item').length; i4689++) {
if ($('.carousel-4689 div.item')[i4689].hasClass('video-slide')) {
//i is the position of the video slide
console.log(i4689);
}
}

sai lầm

$(...)[i4689] is undefined

点击功能

$('.po-4689 .link-to-video"').on('click', function() {
$('.carousel-4689').carousel(*[numberHere]*);
});

1 Câu trả lời

使用 JQuery 选择器,如下所示:

for(var i = 0; i <= $('div.item').length; i++) {
if ($('div.item')[i].hasClass('video-slide')) {
//i is the position of the video slide
}
}

它所做的是使用 for 循环并迭代 div.item 元素的数量。当发现具有 video-slide 类时,内部 block 就会执行。您还可以使用以下代码来执行此操作:

$('div.item').each(function(){
if ($(this).hasClass('video-slide')) {
//$(this) is the video slide (not the position, but the actual element)
}
})

关于javascript - 获取父级中的 div 数量,然后获取特定类的 div 数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39017687/

37 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com