开发者

jQuery delegate optimization

what is the best way to optimize the following snippets that uses delegate()?

jQuery('.menu').delegate('li.gallery', 'hover', function () { jQuery(this).children('.submenu').toggleClass('hide show'); });
jQuery('.menu').delegate('li.interior', 'hover', function () { jQuery(this).children('.submenu').toggleClass('hide show'); });
jQuery('.menu').delegate('li.exterior', 'hover', function () { jQuery(this).children('.submenu').toggleClass('hide show'); });


<li class="gallery"> 
                    <span>gallery</span> 
                    <ul class="submenu hide"> 
                        <li class="interior"> 
                            <a href="/gallery/interior">Interior</a> 
                            <ul class="submenu hide"> 
                                <li><a href="/gallery/interior?gallery=master-bedroom">Master Bedroom</a></li> 
                                <li><a href="/gallery/interior?gallery=living-room">Living Room</a></li> 
                                <li><a href="/gallery/interior?gallery=dining-room">Dining Room</a></li> 
                                <li><a href="/gallery/interior?gallery=kitchen">Kitchen</a></li> 
                                <li><a href="/gallery/interior?gallery=bathroom">Bathroom</a></li> 
                                <li><a href="/gallery/interior?gallery=foyer">Foyer</a></li> 
                                <li><a href="/gallery/interior?gallery=study">Study</a></li> 
                                <li><a href="/gallery/interior?gallery=sunroom">Sunroom</a></li>                                
                                <li><a href="/gallery/interior?gallery=guest-room">Guest Room</a></li>                                                              
                            </ul> 
         开发者_高级运维               </li> 
                        <li class="exterior"> 
                            <a href="/gallery/exterior">Exterior</a> 
                            <ul class="submenu hide"> 
                                <li><a href="/gallery/exterior?gallery=landscapes">Landscape</a></li> 
                                <li><a href="/gallery/exterior?gallery=gardens">Gardens</a></li> 
                                <li><a href="/gallery/exterior?gallery=cottages">Cottages</a></li> 
                                <li><a href="/gallery/exterior?gallery=entry-driveway">Entry/Driveway</a></li>                                                                                              
                            </ul> 
                        </li>                       
                    </ul> 
                </li>


You can use a multiple selector (,) in the .delegate() selector parameter.

However, due to what I would consider a bug in how .delegate() works (off mouseover/mouseout, which fires when entering a child) I'd steer clear it it for now and bind directly, like this:

jQuery('.menu').find('li.gallery, li.interior, li.exterior').hover(function () {
  jQuery(this).children('.submenu').toggleClass('hide show'); 
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜