开发者

jQuery delegate optimization

开发者 https://www.devze.com 2023-01-21 13:57 出处:网络
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(\'hid

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

精彩评论

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