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 に答える