开发者

How can I remove a class from certain position?

I have this function

$('*').hover(

    function (e) {
        mouseX = e.pageX;
        mouseY = e.pageY;
        var elem = document.elementFromPoint(mouseX, mouseY);
        $(elem).addClass('hoverElem');

    },
    function (e) {
        mouseX = e.pageX;
        mouseY = e.pageY;
        var elem = document.elementFromPoint(mouseX, mouseY);
        $(elem).removeClass('hoverElem');
    }
);

this works fine开发者_Go百科 with the first function but the second function doesnt do any thing, any ideas how can i fix it?


Try this instead:

$('*').hover(
    function (e) {
        // Remove class from previous element
        $('.hoverElem').removeClass('hoverElem');

        // Add class to new element
        mouseX = e.pageX;
        mouseY = e.pageY;
        var elem = document.elementFromPoint(mouseX, mouseY);
        $(elem).addClass('hoverElem');
    }
);

or this:

$('*').hover(
    function (e) {
        // Add class to new element
        mouseX = e.pageX;
        mouseY = e.pageY;
        var elem = document.elementFromPoint(mouseX, mouseY);
        $(elem).addClass('hoverElem');
    }, function (e) {
        // Remove class from previous element
        $('.hoverElem').removeClass('hoverElem');
    }
);

I'm not exactly sure what you're trying to do, so it's a bit hard to guess.


here is solution:

$('*').hover(
function (e) {
    $(this).addClass('hoverElem');
},
function (e) {
    $(this).removeClass('hoverElem');
}
);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜