2013-03-29 17 views
0

我發現了一個類似於我的問題的問題,但答案對我無效。我有一個我在網上找到的與JQuery一起使用的導航。在遠程文件中拋棄javascript執行腳本

我把導航執行腳本放在一個外部文件中並將其鏈接起來,但現在它不起作用。有人可以解釋爲什麼它不會工作?我也試過$(文件)。就緒(函數,要麼沒有工作。

我把參考文件中的頭

我已經更新了我的Javascript,這並不工作,要麼

$(document).ready(function() { 
     $(function() { 
      $('#hn_menu > li').bind('mouseenter',function(){ 
     var $elem = $(this); 
      $elem.find('img') 
     .stop(true) 
     .animate({ 
     //'width':'170px', 
     'width':'120px', 
     //'height':'170px', 
     'height':'120px', 
     'left':'0px' 
    },400,'easeOutBack') 
    .andSelf() 
    .find('.hn_wrap') 
    blah blah blah 
     }); 
    }); 

這裏的整個文件。我需要這個爛攤子轉換到外部文件的腳本

<html> 
<head> 
    <title>Slide Down Box Menu with jQuery and CSS3</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
    <meta name="description" content="Slide Down Box Menu with jQuery and CSS3" /> 
    <meta name="keywords" content="jquery, css3, sliding, box, menu, cube, navigation, portfolio, thumbnails"/> 
    <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon"/> 
    <link rel="stylesheet" href="css/style.css" type="text/css" media="screen"/> 
    <style> 
     body{ 
      background:#333 url(bg.jpg) repeat top left; 
      font-family:Arial; 
     } 
     span.reference{ 
      position:fixed; 
      left:10px; 
      bottom:10px; 
      font-size:12px; 
     } 
     span.reference a{ 
      color:#aaa; 
      text-transform:uppercase; 
      text-decoration:none; 
      text-shadow:1px 1px 1px #000; 
      margin-right:30px; 
     } 
     span.reference a:hover{ 
      color:#ddd; 
     } 
     ul.sdt_menu{ 
      margin-top:150px; 
     } 
     h1.title{ 
      text-indent:-9000px; 
      background:transparent url(title.png) no-repeat top left; 
      width:633px; 
      height:69px; 
     } 
    </style> 
    <script type="text/javascript" src="testexec.js"></script> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
    <script type="text/javascript" src="jquery.easing.1.3.js"></script> 
</head> 

<body> 
    <div class="content"> 
     <h1 class="title">Slide Down Box Menu with jQuery and CSS3</h1> 
     <ul id="sdt_menu" class="sdt_menu"> 
      <li> 
       <a href="#"> 
        <img src="images/2.jpg" alt=""/> 
        <span class="sdt_active"></span> 
        <span class="sdt_wrap"> 
         <span class="sdt_link">About me</span> 
         <span class="sdt_descr">Get to know me</span> 
        </span> 
       </a> 
      </li> 
      <li> 
       <a href="#"> 
        <img src="images/1.jpg" alt=""/> 
        <span class="sdt_active"></span> 
        <span class="sdt_wrap"> 
         <span class="sdt_link">Portfolio</span> 
         <span class="sdt_descr">My work</span> 
        </span> 
       </a> 
       <div class="sdt_box"> 
         <a href="#">Websites</a> 
         <a href="#">Illustrations</a> 
         <a href="#">Photography</a> 
       </div> 
      </li> 
      <li> 
       <a href="#"> 
        <img src="images/3.jpg" alt=""/> 
        <span class="sdt_active"></span> 
        <span class="sdt_wrap"> 
         <span class="sdt_link">Inspiration</span> 
         <span class="sdt_descr">Where ideas get born</span> 
        </span> 
       </a> 
      </li> 
      <li> 
       <a href="#"> 
        <img src="images/4.jpg" alt=""/> 
        <span class="sdt_active"></span> 
        <span class="sdt_wrap"> 
         <span class="sdt_link">Photos</span> 
         <span class="sdt_descr">I like to photograph</span> 
        </span> 
       </a> 
      </li> 
      <li> 
       <a href="#"> 
        <img src="images/5.jpg" alt=""/> 
        <span class="sdt_active"></span> 
        <span class="sdt_wrap"> 
         <span class="sdt_link">Blog</span> 
         <span class="sdt_descr">I write about design</span> 
        </span> 
       </a> 
      </li> 
      <li> 
       <a href="#"> 
        <img src="images/6.jpg" alt=""/> 
        <span class="sdt_active"></span> 
        <span class="sdt_wrap"> 
         <span class="sdt_link">Projects</span> 
         <span class="sdt_descr">I like to code</span> 
        </span> 
       </a> 
       <div class="sdt_box"> 
        <a href="#">Job Board Website</a> 
        <a href="#">Shopping Cart</a> 
        <a href="#">Interactive Maps</a> 
       </div> 
      </li> 
     </ul> 
    </div> 
    <div> 
     <span class="reference"> 

更多的腳本和CSS樣式 :www.htmldrive.net

<!-- The JavaScript --> 
    <!-- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
    <script type="text/javascript" src="jquery.easing.1.3.js"></script>--> 
    <script type="text/javascript"> 
     $(function() { 
      /** 
      * for each menu element, on mouseenter, 
      * we enlarge the image, and show both sdt_active span and 
      * sdt_wrap span. If the element has a sub menu (sdt_box), 
      * then we slide it - if the element is the last one in the menu 
      * we slide it to the left, otherwise to the right 
      */ 
      $('#sdt_menu > li').bind('mouseenter',function(){ 
       var $elem = $(this); 
       $elem.find('img') 
        .stop(true) 
        .animate({ 
         'width':'170px', 
         'height':'170px', 
         'left':'0px' 
        },400,'easeOutBack') 
        .andSelf() 
        .find('.sdt_wrap') 
        .stop(true) 
        .animate({'top':'140px'},500,'easeOutBack') 
        .andSelf() 
        .find('.sdt_active') 
        .stop(true) 
        .animate({'height':'170px'},300,function(){ 
        var $sub_menu = $elem.find('.sdt_box'); 
        if($sub_menu.length){ 
         var left = '170px'; 
         if($elem.parent().children().length == $elem.index()+1) 
          left = '-170px'; 
         $sub_menu.show().animate({'left':left},200); 
        } 
       }); 
      }).bind('mouseleave',function(){ 
       var $elem = $(this); 
       var $sub_menu = $elem.find('.sdt_box'); 
       if($sub_menu.length) 
        $sub_menu.hide().css('left','0px'); 

       $elem.find('.sdt_active') 
        .stop(true) 
        .animate({'height':'0px'},300) 
        .andSelf().find('img') 
        .stop(true) 
        .animate({ 
         'width':'0px', 
         'height':'0px', 
         'left':'85px'},400) 
        .andSelf() 
        .find('.sdt_wrap') 
        .stop(true) 
        .animate({'top':'25px'},500); 
      }); 
     }); 
    </script> 
</body> 
+0

應罰款假設到文件的路徑是正確的和文件包含在jquery.js之後。瀏覽器控制檯中引發的任何錯誤?使用控制檯也可以驗證路徑是否正確 – charlietfl

回答

0

有一對夫婦的事情,可能是問題。這是一個基本方針遵循:

寫在<head>標籤這樣的<script>鏈接:

<head> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> 
    <script src="/your-root-folder/your-script.js"></script> 
</head> 

然後,在your-script.js,你需要用的功能在$(文件)。就緒(函數(){...}

$(document).ready(function(){ 
    //call the function here 
}); 

編輯

你的問題是爲了我你已經鏈接你的腳本文件。 jQuery鏈接和Easing鏈接需要放在腳本之前。在我給出your-script.js的示例中,請注意jquery.min.js之後。

複製和取代一切:

的HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
    <head> 
     <title>Slide Down Box Menu with jQuery and CSS3</title> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
     <meta name="description" content="Slide Down Box Menu with jQuery and CSS3" /> 
     <meta name="keywords" content="jquery, css3, sliding, box, menu, cube, navigation, portfolio, thumbnails"/> 
    <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon"/> 
    <link rel="stylesheet" href="css/style.css" type="text/css" media="screen"/> 
    <style> 
     body{ 
      background:#333 url(bg.jpg) repeat top left; 
      font-family:Arial; 
     } 
     span.reference{ 
      position:fixed; 
      left:10px; 
      bottom:10px; 
      font-size:12px; 
     } 
     span.reference a{ 
      color:#aaa; 
      text-transform:uppercase; 
      text-decoration:none; 
      text-shadow:1px 1px 1px #000; 
      margin-right:30px; 
     } 
     span.reference a:hover{ 
      color:#ddd; 
     } 
     ul.sdt_menu{ 
      margin-top:150px; 
     } 
     h1.title{ 
      text-indent:-9000px; 
      background:transparent url(title.png) no-repeat top left; 
      width:633px; 
      height:69px; 
     } 
    </style> 
    <!-- The JavaScript --> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
    <script type="text/javascript" src="jquery.easing.1.3.js"></script> 
    <script type="text/javascript" src="test.js"></script> 
</head> 

<body> 
    <div class="content"> 
     <h1 class="title">Slide Down Box Menu with jQuery and CSS3</h1> 
     <ul id="sdt_menu" class="sdt_menu"> 
      <li> 
       <a href="#"> 
        <img src="images/2.jpg" alt=""/> 
        <span class="sdt_active"></span> 
        <span class="sdt_wrap"> 
         <span class="sdt_link">About me</span> 
         <span class="sdt_descr">Get to know me</span> 
        </span> 
       </a> 
      </li> 
      <li> 
       <a href="#"> 
        <img src="images/1.jpg" alt=""/> 
        <span class="sdt_active"></span> 
        <span class="sdt_wrap"> 
         <span class="sdt_link">Portfolio</span> 
         <span class="sdt_descr">My work</span> 
        </span> 
       </a> 
       <div class="sdt_box"> 
         <a href="#">Websites</a> 
         <a href="#">Illustrations</a> 
         <a href="#">Photography</a> 
       </div> 
      </li> 
      <li> 
       <a href="#"> 
        <img src="images/3.jpg" alt=""/> 
        <span class="sdt_active"></span> 
        <span class="sdt_wrap"> 
         <span class="sdt_link">Inspiration</span> 
         <span class="sdt_descr">Where ideas get born</span> 
        </span> 
       </a> 
      </li> 
      <li> 
       <a href="#"> 
        <img src="images/4.jpg" alt=""/> 
        <span class="sdt_active"></span> 
        <span class="sdt_wrap"> 
         <span class="sdt_link">Photos</span> 
         <span class="sdt_descr">I like to photograph</span> 
        </span> 
       </a> 
      </li> 
      <li> 
       <a href="#"> 
        <img src="images/5.jpg" alt=""/> 
        <span class="sdt_active"></span> 
        <span class="sdt_wrap"> 
         <span class="sdt_link">Blog</span> 
         <span class="sdt_descr">I write about design</span> 
        </span> 
       </a> 
      </li> 
      <li> 
       <a href="#"> 
        <img src="images/6.jpg" alt=""/> 
        <span class="sdt_active"></span> 
        <span class="sdt_wrap"> 
         <span class="sdt_link">Projects</span> 
         <span class="sdt_descr">I like to code</span> 
        </span> 
       </a> 
       <div class="sdt_box"> 
        <a href="#">Job Board Website</a> 
        <a href="#">Shopping Cart</a> 
        <a href="#">Interactive Maps</a> 
       </div> 
      </li> 
     </ul> 
    </div> 
    <div> 
      <span class="reference">More script and css style: <a href="http://www.htmldrive.net/" title="HTML DRIVE - Free DHMTL Scripts,Jquery plugins,Javascript,CSS,CSS3,Html5 Library">www.htmldrive.net</a>    
      </span> 
     </div> 
    </body> 
</html> 

我叫test.js的外部JavaScript:

// JavaScript Document 
$(function() { 
      /** 
      * for each menu element, on mouseenter, 
      * we enlarge the image, and show both sdt_active span and 
      * sdt_wrap span. If the element has a sub menu (sdt_box), 
      * then we slide it - if the element is the last one in the menu 
      * we slide it to the left, otherwise to the right 
      */ 
      $('#sdt_menu > li').bind('mouseenter',function(){ 
       var $elem = $(this); 
       $elem.find('img') 
        .stop(true) 
        .animate({ 
         'width':'170px', 
         'height':'170px', 
         'left':'0px' 
        },400,'easeOutBack') 
        .andSelf() 
        .find('.sdt_wrap') 
        .stop(true) 
        .animate({'top':'140px'},500,'easeOutBack') 
        .andSelf() 
        .find('.sdt_active') 
        .stop(true) 
        .animate({'height':'170px'},300,function(){ 
        var $sub_menu = $elem.find('.sdt_box'); 
        if($sub_menu.length){ 
         var left = '170px'; 
         if($elem.parent().children().length == $elem.index()+1) 
          left = '-170px'; 
         $sub_menu.show().animate({'left':left},200); 
        } 
       }); 
      }).bind('mouseleave',function(){ 
       var $elem = $(this); 
       var $sub_menu = $elem.find('.sdt_box'); 
       if($sub_menu.length) 
        $sub_menu.hide().css('left','0px'); 

       $elem.find('.sdt_active') 
        .stop(true) 
        .animate({'height':'0px'},300) 
        .andSelf().find('img') 
        .stop(true) 
        .animate({ 
         'width':'0px', 
         'height':'0px', 
         'left':'85px'},400) 
        .andSelf() 
        .find('.sdt_wrap') 
        .stop(true) 
        .animate({'top':'25px'},500); 
      }); 
     }); 
+0

我不知道我錯過了什麼,我使用了我從中獲得的源文件並進行了這些更改,但它不起作用。我添加了$(document).ready(function(){粘貼在上面的執行體中,也是在腳本源代碼的上面。我也把它放在頭部。我得到了一切,但仍有一些不妥之處我會發佈菜單的源代碼 – Autonomic

+0

您是否從所有代碼中刪除了$(function(){}); –

+2

@JakeZeitz'$(function(){}) '是'$(document).ready(function(){})的簡寫'' –