简体   繁体   English

将图像悬停在图像上时jQuery中的问题?

[英]Problems in jQuery when hovering an image over the image?

I have a page listing the images. 我有一个页面列出这些图像。 And when I hover on that image it will display the play icon over the image. 当我将鼠标悬停在该图像上时,它将在图像上方显示播放图标。

I have written some jQuery code for that: 我为此写了一些jQuery代码:

$('#hovervideo').mouseenter(function (){
    var isviddiv = $(this).find('.video-thumb');
    if(isviddiv.length){
        isviddiv.css('display','block');
    }
        console.log(isviddiv.length);
});
$('#hovervideo').mouseleave(function (){
    var isviddiv = $(this).find('.video-thumb');
    if(isviddiv.length){
        isviddiv.css('display','none');
    }
        console.log(isviddiv.length);
});

Html Part HTML部分

<h1 class="productvideo" id="hovervideo"><a href="#"><img src="images/tst-img.jpg" alt="" /></a>
                <span class="video-thumb"></span>
                </h1>

but it will work only for one image. 但仅适用于一张图片。 bcoz the id "hovervideo" is same for all the H1. bcoz的ID“ hovervideo”对于所有H1都是相同的。 but i want it in all the image hover effect. 但是我想要所有的图像悬停效果。

Can any one suggest me what to do. 谁能建议我该怎么办。

Thanks 谢谢

Use the class selector instead, add the class "test-image" to all images as so: 改用类选择器,将类“ test-image”添加到所有图像中,如下所示:

<h1 class="productvideo" id="hovervideo"><a href="#"><img src="images/tst-img.jpg" class="test-image" alt="" /></a>
                <span class="video-thumb"></span>
                </h1>

Then change the script to use the class selector 然后更改脚本以使用类选择器

$('.test-image').mouseenter(function (){
    var isviddiv = $(this).find('.video-thumb');
    if(isviddiv.length){
        isviddiv.css('display','block');
    }
        console.log(isviddiv.length);
});
$('.test-image').mouseleave(function (){
    var isviddiv = $(this).find('.video-thumb');
    if(isviddiv.length){
        isviddiv.css('display','none');
    }
        console.log(isviddiv.length);
});

You cannot have more than one element with the same ID. 具有相同ID的元素不能超过一个。 Id should be a unique identifier. ID应该是唯一的标识符。 If you want the event to be bound to multiple elements, simply select them by class: 如果要将事件绑定到多个元素,只需按类选择它们:

$('.productvideo')...

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

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