简体   繁体   English

JQuery - 单击并悬停在同一个函数上

[英]JQuery - click and hover on same function

I have this jQuery script: 我有这个jQuery脚本:

$('img[data-hover]').hover(function() {
    $(this)
        .attr('tmp', $(this).attr('src'))
        .attr('src', $(this).attr('data-hover'))
        .attr('data-hover', $(this).attr('tmp'))
        .removeAttr('tmp');
}).each(function() {
    $('<img />').attr('src', $(this).attr('data-hover'));

});  

It works fine, when I hove the images, the hover works fine. 它工作正常,当我在图像上,悬停工作正常。

But now I need to start the hover again, when I click on certain boxes, on those: 但现在我需要再次开始悬停,当我点击某些方框时,在那些:

$('#first, #second, #third').click(function(){  .....  

Is it somehow possible to start the hover "on click" and keep the actual hover function? 是否有可能以“点击”开始悬停并保持实际的悬停功能? Or even modify it to a "function"? 或者甚至将其修改为“功能”?

I've tried it but I failed. 我试过了,但我失败了。
Thanks. 谢谢。

EDIT: 编辑:
here's the HTML code with the images: 这是带有图像的HTML代码:

<div class="auswahlbox">
        <div class="auswahl" id="first" data-id="1">
            <div class="bild">
                <img src="https://image.jpg" data-hover="https://images_hover.jpg" />
            </div>
            <div class="box">
                <p>Text</p>
            </div>
        </div>
        <div class="auswahl" id="second" data-id="2">
            <div class="bild">
                <img src="https://image.jpg" data-hover="https://images_hover.jpg" />
            </div>
            <div class="box">
                <p>Text</p>
            </div>
        </div>
        <div class="auswahl" id="third" data-id="3">
            <div class="bild">
                <img src="https://image.jpg" data-hover="https://images_hover.jpg" />
            </div>
            <div class="box">
                <p>Text</p>
            </div>
        </div>
    </div>    

Situation now: 现在的情况:
When I hove the images in .bild it works fine. 当我在.bild中移动图像时,它工作正常。

What I need: 我需要的:
When I click the "<div class="auswahl" id="first" data-id="1">" (second, third) div, the hover of the images shall "start" once. 当我点击"<div class="auswahl" id="first" data-id="1">" (第二,第三)div时,图像的悬停应该“开始”一次。

Do you mean something like this: 你的意思是这样的:

$('#first, #second, #third').click(function(){
    $('img[data-hover]').trigger('mouseover');
});

When #first, #second #third are clicked it triggers the hover event on img[data-hover] . #first, #second #third被点击时,它会触发img[data-hover]上的悬停事件。

To just trigger images in the same div try: 要在同一个div触发图像,请尝试:

$('#first, #second, #third').click(function(){
    $(this).find('img[data-hover]').trigger('mouseover');
});

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

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