0

im trying to call .bind("click") from an external .js file (js1.js).

here is the code:

js1.js:

$(function() {
$("p").bind("click",function(){
  alert("The paragraph was clicked.");
});
});

page.html:

<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="./css/mystyle.css" />
<link rel="stylesheet" href="./css/global.css">
<script type="text/javascript" src="./js/js1.js"></script>
<title>New WebPage</title>
</head>
<body>
<p>Click me!</p>
</body>

this code does not work in my files.

i found it from w3scools and it is working in their site:

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").bind("click",function(){
alert("The paragraph was clicked.");
});
});
</script>
</head>
<body>
<p>Click me!</p>
</body>
</html>

how can i make it running from my external .js? any ideas?

thanks.

4

2 回答 2

0

从外观上看,您还没有在页面上添加 jQuery。在您的脚本之前添加 jQuery。

<link rel="stylesheet" type="text/css" media="screen" href="./css/mystyle.css" />
<link rel="stylesheet" href="./css/global.css">
<script type="text/javascript" src="path/to/jQuery.js"></script>
<script type="text/javascript" src="./js/js1.js"></script>
<title>New WebPage</title>
于 2013-03-03T13:29:48.803 回答
0

如果您忘记添加此行

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

所以<head> </head>先把它添加到你的部分。否则你的脚本不能工作。波纹管为我工作的完整代码..

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#p').bind('click', function(){
    alert('The paragraph was clicked.');
});
});
</script>
<style>
#p{cursor:pointer;}
</style>
</head>
<body>
<p id="p">Click me!</p>
</body>
</html>
于 2013-03-03T13:32:05.613 回答