0

我想将以下 javascript 从鼠标悬停事件调整为页面加载事件,但是到目前为止我的尝试都失败了。所以澄清一下,当页面加载时,我希望脚本激活。这是代码:

$(function() {
            $('#sdt_menu2 > li').bind('mouseenter',function(){
                var $elem = $(this);
                $elem.find('img')
                     .stop(true)
                     .andSelf()
                     .find('.sdt_wrap')
                     .stop(true)
                     .andSelf()
                     .find('.sdt_active')
                     .stop(true)
                     .animate({'height':'45px'},300,function(){
                    var $sub_menu = $elem.find('.sdt_box');
                    if($sub_menu.length){   
                    }   
                });
4

1 回答 1

1

If I understood the quesiton correct, it's as easy as

$(function(){
            var $elem = $(this);
            $elem.find('img')
                 .stop(true)
                 .andSelf()
                 .find('.sdt_wrap')
                 .stop(true)
                 .andSelf()
                 .find('.sdt_active')
                 .stop(true)
                 .animate({'height':'45px'},300,function(){
                var $sub_menu = $elem.find('.sdt_box');
                if($sub_menu.length){   
                }   
            });


$(handler) (that you used already) actually is a shortcut for $(document).ready(handler).
From the jQuery Documentation on .ready():

All three of the following syntaxes are equivalent:
$( document ).ready( handler )
$().ready( handler ) (this is not recommended)
$( handler )



[Edit]: If the function should still work on $('#sdt_menu2 > li'), the $elem (is this even a valid name? Oo) has to be set to that, of course. So if that'S what you want, substitute

var $elem = $(this);

by

var $elem = $('#sdt_menu2 > li')`
于 2013-11-14T00:16:46.250 回答