Is it bad practice to do $("*").live("mouseover", someFun) in jQuery?
There will sometimes be a lot of mouseover events and the $("*") selector may be expensive. Will this slow my pages to a crawl on slow machines running IE6?
Is there a better way to do this? I want to know about 开发者_JAVA技巧every mouseover event that happens on a page.
Just do $('body')
. That will assign a single handler to the <body>
element, and every descendant element (so, every element in the page) will bubble it's mouseover event up to that point. All you need to do inside the handler is check the originator of the event to find the exact element:
$('body').mouseover(function(e) {
var sender = e.target;
//sender is the element who was moused over
});
The key is not to do anything too intensive inside that handler, since it will basically be firing constantly as the user moves the mouse across your page. Best to start by checking the most restrictive conditions possible and return out of the method early as often as you can.
I'm going to go out on a limb here and say that, judging by the way looking at that expression makes my teeth hurt, it may in fact be bad practice.
But if you absolutely must know about every mouseover ever, it may be the best thing available. I would, however, question that perceived need and hypothesize that the objectives you believe it will fulfill for you may be achieved in a superior fashion.
精彩评论