2017-04-13 35 views
-7

我有一个小程序的HTML和JS程序使用我的JavaScript jQuery的不工作

HTML:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<script type="text/javascript" src="temp.js"></script> 
</head> 
<body> 
<div id='display'></div> 
<input type="button" id="creation" value="Create"/> 
</body>  

temp.js

$(document).ready(function() { 
    $('#creation').click(function() { 
    alert("hi"); 
    }); 
}); 

任何一个可以帮助我找到错误在这个上面的程序中。当我点击创建按钮它应该提醒我“嗨”。感谢您的帮助

+5

你似乎并没有加载jQuery的。 –

+0

你能多解释一下吗 – Jeevan

+2

jQuery不是内置于浏览器的。 –

回答

1

您需要加载jQuery。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
2

你没有在你的代码包括jQuery的:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script type="text/javascript" src="temp.js"></script> 

确保它是第一参考jQuery之前包括在内。

0

您需要添加jQuery然后加载您的temp.js脚本。

使用jQuery的工作示例。

$(document).ready(function() { 
 
    $('#creation').click(function() { 
 
    alert("hi"); 
 
    }); 
 
});
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>JQuery</title> 
 
</head> 
 
<body> 
 
<div id='display'></div> 
 
<input type="button" id="creation" value="Create"/> \t 
 
<script src="https://code.jquery.com/jquery-3.1.0.js"></script> 
 

 
</body> 
 
</html>