简体   繁体   English

使用JavaScript定位相同类/ ID的多个实例

[英]Targeting multiple instances of the same class/ID with Javascript

I'm trying at the moment to target multiple instances of the same div class ( .overlay ) - the basic idea I'm trying to execute is that each div contains a HTML5 video inside another wrapped div which on mouseenter reveals itself, sets the video timeline to 0 and plays, and on mouseout resets the video to 0 again. 我目前正在尝试定位同一个div类( .overlay )的多个实例-我要执行的基本思想是,每个div都在另一个包装的div中包含一个HTML5视频,在mouseenter显示自身,设置视频时间轴设置为0并播放,并在mouseout将视频重新设置为0。

The problem I'm having is that only the first item of my grid works at the moment with nothing happening on the rollover of the others. 我遇到的问题是,目前只有网格的第一项起作用,而其他项的翻转没有任何反应。 This is my Javascript: 这是我的Javascript:

$(document).ready(function() {
            $('.overlay').mouseenter(function(){
            $('#testvideo').get(0).play();
            }).mouseout(function() {
            $('#testvideo').get(0).pause();
            $('#testvideo').get(0).currentTime = 0;
            })
        });

I've also tried the following 我也尝试了以下

$(document).ready(function() {
            $('.overlay').mouseenter.each(function(){
            $('#testvideo').get(0).play();
            }).mouseout(function() {
            $('#testvideo').get(0).pause();
            $('#testvideo').get(0).currentTime = 0;
            })
        });

but that simply broke the functionality all together! 但这完全破坏了功能!

Here is a fiddle showing what should happen: http://jsfiddle.net/jameshenry/ejmfydfy/ 这是显示应该发生什么的小提琴: http : //jsfiddle.net/jameshenry/ejmfydfy/

The only difference between this and the actual site is that there are multiple grid items and thumbnails. 该站点与实际站点之间的唯一区别是存在多个网格项和缩略图。 I also don't want the behaviour to by asynchronous but rather individual - does anyone have any idea where I'm going wrong (I'm guessing my javascript!) 我也不希望这种行为是异步的,而是个人的-任何人都不知道我要去哪里哪里(我猜我的JavaScript!)

The problem is that you're using always $('#testvideo') , independent on the div you're entering the mouse. 问题是您总是使用$('#testvideo') ,与您输入鼠标的div无关。 Since the HTML's id property must be unique, only the first element that you set the id testvideo will work the way you expect. 由于HTML的id属性必须是唯一的,因此只有您设置id testvideo的第一个元素可以按您期望的方式工作。

You should be using the video tag referenced by the div.overlay , or you could add a CSS class to the video tags, so you could use that class to find the video. 您应该使用div.overlay引用的video标签,或者可以将CSS类添加到video标签中,以便可以使用该类来查找视频。

The code below will get the overlayed video, independent of which it is. 下面的代码将获取叠加的视频,而与它无关。

$(document).ready(function() {
  $('.overlay').hover(function() {
    $(this).parent().find('video').get(0).play();
  }, function() {
    var video = $(this).parent().find('video').get(0);
    video.pause();
    video.currentTime = 0;
  });
});

Take a look at your updated fiddle . 一下您更新的小提琴

The first way you did it should work, the second one not. 第一种方法应该可以,第二种则不能。 But, you shouldn't use an id like #testvideo if there are a lot of videos (one on each .overley element). 但是,如果有很多视频(每个.overley元素一个),则不应使用#testvideo这样的ID。 Having multiple instances of the same id produce unexpected behaviuor, like "only working on the first item" . 具有相同id的多个实例会产生意外的行为,例如“仅在第一个项目上工作”

You should change your #testvideo with .testvideo and change your code to something like this: 你应该改变你的#testvideo.testvideo和更改您的代码是这样的:

$(document).ready(function() {
    $('.overlay').mouseenter(function(){
        $(this).find('.testvideo').play();
    }).mouseout(function() {
        $(this).find('.testvideo').pause();
        $(this).find('.testvideo').currentTime = 0;
    })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM