Simple Mouseover Function in jQuery
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);
});
精彩评论