开发者

Simple Mouseover Function in jQuery

开发者 https://www.devze.com 2023-03-08 18:11 出处:网络
I have the following HTML: img class="hoverContact" img class="hoverContact" img class="hoverContact"

I have the following HTML:

img class="hoverContact"
img class="hoverContact"
img class="hoverContact"

And the following jQuery:

function highlightContact(ev) {
    $(开发者_运维知识库this).addClass('lightblue');  
}

$('.hoverContact').mouseover(function(){
    highlightContact();
});

Any suggestions?


use the apply() method so that this within your highlightContrast() function refers to the relevant img tag.

function highlightContact(ev) {
    $(this).addClass('lightblue');  
}

$('.hoverContact').mouseover(function(){
    highlightContact.apply(this);
});

Alternatively, if you don't need that ev at all - if all you are doing is adding that class, you can get rid of the function highlightContact(){...} completely and simply use:

$('.hoverContact').mouseover(function(){
    $(this).addClass('lightblue');
});


You're trying to use this inside highlightContact when the function has no context. Also, the function takes a paramater ev but you're not passing it in. Instead, pass this in from the mouseover function and reference ev instead of this in the highlightContact function:

function highlightContact(ev) {
        $(ev).addClass('lightblue');  
    }

    $('.hoverContact').mouseover(function(){
        highlightContact(this);
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消