开发者

How to properly move jQuery from index page, into separate javascript file?

How can I move my initialization code i.e.

  <script type="text/javascript">
        $(document).ready(function() {
            var t; // This will be a timeout
            $('.navHover').mouseover(function() {
                if (t)
                {
                    clearTimeout(t);
                }

                $('.navigation').slideDown(400);
            }).mouseout(function() {
                t = setTimeout(function() {
                    $('.navigation').slideUp(400);
                }, 800); // .8 second delay before hiding
            });

            $('.navigation开发者_开发知识库').mouseover(function() {
                if (t)
                {
                    clearTimeout(t);
                }
            }).mouseout(function() {
                t = setTimeout(function() {
                    $('.navigation').slideUp(400);
                }, 800); // .8 second delay before hiding
            });
        });
    </script>

Into a separate file? main.js?

Thanks


Simply create a text file, paste that code into it (minus the script tags) and add :

<script type="text/javascript" src="path-to\main.js"></script>

Into the <head> of your page.


What do you mean properly? Because it's in a document ready handler, you can safely just paste that code into a separate file and include it in the head of your page.


This may seem too easy of a solution but can't you just copy from $(document) through to the closing tags '});' and paste that into a separate js file? Then load that file the way you would normally by using

<script type="text/javascript" src="main.js"></script>


Put it in a JS file and include that JS file the same way you include your jQuery file. Main will work fine.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜