开发者

How to 'Turn Off', or 'Override' a Named Function

开发者 https://www.devze.com 2023-03-09 02:31 出处:网络
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 jQuery code:

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

and

$('.hoverContact').live("mouseover", function(){
    highlightContact(this);
});
$('.hoverContact').live("mouseout", function(){
    un开发者_运维百科highlightContact(this);
});
$('.hoverContact').live("click", function(){
    highlightContact(this);
});

And I am trying to keep the lightblue class on a click, but obviously when I click I need to mouseaway and it turns off the lightblue class... so I need help figuring out how to override the unhighlight class...


There might be a better way but I would do it like this - you can set arbitrary properties with jQuery and not screw up your page validation or browser compatibility by sticking data in a randomly named html attribute. The "proper" way to do that is to include another XML namespace on the HTML tag but I've never seen a practical reason to do it.

$('.hoverContact').attr('custom:on', '0')
.live("mouseover", function(){
    highlightContact(this);
})
.live("mouseout", function(){
    if ($(this).attr('custom:on') == '0') {
      unhighlightContact(this);
    }
})
.live("click", function(){
    $('.hoverContact').attr('custom:on', '0');
    $(this).attr('custom:on', '1');
    highlightContact(this);
});
0

精彩评论

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