简体   繁体   English

jQuery脚本在HTML中有效,但在外部文件中无效

[英]jQuery script works when in HTML but not in external file

I have a test using jquery that s work fine when in html file but when I put it in anexternal js file it doesn t work , help please , I need it to continue my application. 我有一个使用jQuery的测试,在html文件中工作正常,但是当我将其放入外部js文件中时,它不起作用,请帮助,我需要它来继续我的应用程序。 query.js is in /js directory query.js在/ js目录中

HTML : detail.jsp HTML:detail.jsp

<%@ taglib uri="http://www.springframework.org/tags/form"  prefix="springform"  %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html  >
<head>
<meta charset="utf-8">
<title>Ajouter annonce</title>
<link type="text/css" href="/bootstrap/dist/css/bootstrap.css" rel="stylesheet" >
<link type="text/css" href="/css/style.css" rel="stylesheet" >

<script  src="https://code.jquery.com/jquery-2.1.1.js"></script>

<script type="text/javascript" src="js/query.js"></script>
<script type="text/javascript" src="js/main.js"></script> 

</head>
<body class="body2">

<div id="test" class="test" > Allo</div>
</body>
</html>

Js file : query.js js文件:query.js

$(document).ready(function(){
    $("#test").click(function(){
        alert("hello");
    })
 });

with the help of the comments, I found the error was in the path : 在评论的帮助下,我发现错误出在路径中:

<script type="text/javascript" src="/js/query.js"></script>

instead of : 代替 :

 <script type="text/javascript" src="js/query.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM