2014-10-17 53 views
0

我的代碼在HTML內工作,但不是從外部.js文件中工作。外部jQuery問題

在我的外部.js中,我有一個類似於.animate()和hide()等的函數,但它只能用於.text()或.html()這樣的函數,但它不能在外部文件中工作。 我甚至刪除了我的所有代碼,只是保留了這個腳本,仍然沒有起作用,但是當我複製粘貼到我的html中時,它工作正常。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
<head> 
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 
<title>Untitled Document</title> 
 
</head> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 

 
    <head> 
 

 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 

 
     <title>Untitled Document</title> 
 
     
 

 
    </head> 
 

 

 
    <body > 
 
     <div id="demo"> --- </div> 
 
     <button>click me</button> 
 

 
    </body> 
 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" type="text/javascript"></script> 
 
    
 
<script type="text/javascript"> 
 
    $("button").click(function(){ 
 
     $("#demo").html("text"); 
 
    }); 
 
</script> 
 

 
</html>

+0

與你的代碼'$(文件)。就緒包裝你的單擊事件處理程序(函數(){你的代碼});' – Regent 2014-10-17 05:35:09

回答

0

在您的JS試試這個

$(function() { 
    $("button").click(function(){ 
     $("#demo").html("text"); 
    }); 
}); 
+0

我複製過去你的代碼我的.js不起作用 我敢肯定,我做了壞事:/ 因爲你的回答是LOGIC – 2014-10-17 05:47:38

+0

@ CID翼檢查您的.js路徑正確或錯誤的 – CSK 2014-10-17 05:49:22

+0

haha​​hahahahahahahahahaahhahahahaahahahahahahahahahahahahah IM是否要瘋了最後一個小時beceause是ahhahahaha的 和我的文件名是red.js – 2014-10-17 05:54:06

0

你的HTML代碼是完全搞砸了!更新下

HTML

<!DOCTYPE html> 
<html>  
<head> 
<title>Test Page</title> 
</head>  
<body> 
    <div id="demo"> --- </div> 
    <button>click me</button> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" type="text/javascript"></script>  
    <script type="text/javascript"> 
     $(function() { 
      $("button").on("click", function() { 
       $("#demo").html("text"); 
      }); 
     }); 
    </script> 
</body>  
</html> 
+0

我知道我只是複製過去我的代碼從Dreamweaver CS6 ,並在我的HTML是工作,我只是想讓這個腳本工作一個外部.js – 2014-10-17 05:43:41

+0

然後你需要包裝你的腳本$(function(){....} ); – Aru 2014-10-17 06:45:57