2014-10-17 88 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