在我的動態Web項目(Eclipse)中,使用Spring MVC框架開發的,我具有下面的JSP頁面,放置在文件夾WEB-INF/jsp /:瀏覽器無法識別包含外部javascript/jquery文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>HorarioLivre</title>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<script src="js/index.js"></script>
<link rel="stylesheet" href="css/style-main.css">
<link rel="stylesheet" href="css/style-popup.css">
</head>
<body>
<header>
<div class="container">
<h1><a href="#">HorarioLivre</a></h1>
<nav>
<ul>
<li><a href="listagem_evento.html" class="icon evento">Eventos</a></li>
<li><a href="cadastra_horario.html" class="icon horario">Cadastrar Horarios</a></li>
<li><a href="listagem_horario.html" class="icon horario">Listar Horarios</a></li>
<li><a href="listagem_usuario.html" class="icon usuario">Usuarios</a></li>
<li><a href="#">${usuario.nome}</a>
<ul>
<li><a href="usuario_perfil.html" class="icon perfil">Perfil</a></li>
<li><a href="usuario_config.html" class="icon settings">Configurações</a></li>
<li><a href="#">Logout</a></li>
</ul>
</li>
</ul>
</nav>
</div>
</header>
<div id="results">
<a href="#" id="close">Fechar</a>
<div id="content"></div>
</div>
</body>
</html>
我的問題是很明顯的應用程序不被讀取的js/index.js文件,放置在文件夾的WebContent/JS(的css文件,放置在的WebContent/CSS,通常讀取)。當我在JSP頁面中直接放入一些javascript/jquery代碼(如問題Browser doesn't recognize javascript/jquery code中顯示的代碼)時,它們將毫無問題地執行。
有人可以找到這個網頁的問題?
更新1
$(document).ready(function(){
setupPopup();
});
function setupPopup() {
$('a').click(function() {
$('#content').load($(this).attr('href'));
$('#container').append('<div id="cover">');
$('#results').fadeIn(500);
popupPosition();
});
$('#close').click(function() {
$('#results').fadeOut(100);
$('#cover').remove();
});
$(window).bind('resize', popupPosition);
}
function popupPosition() {
if(!$("#results").is(':visible')){ return; }
$("#results").css({
left: ($(window).width() - $('#results').width())/2,
top: ($(window).width() - $('#results').width())/7,
position:'absolute'
});
$('#results').draggable();
}
最後更新
最終,我選擇不使用jQuery的,並以此替換頭中的代碼:
<script>
function handleClick(url){
document.getElementById("results").style.display = 'block';
document.getElementById("content").innerHTML='<object type="text/html" data="'+url+'" ></object>';
}
function cleanDiv() {
document.getElementById("results").style.display = 'none';
document.getElementById("content").innerHTML=' ';
}
</script>
每個鏈接都有這樣的格式:
<a href="#" onclick="handleClick('listagem_evento.html')" class="icon evento">Eventos</a>
和我的彈出窗口中的HTML代碼得到這個形式:
<section class="about" id="results" style="left: 183px; top: 111px;" onMouseDown="dragStart(event, 'results');">
<div align="right"><a href="#" class="classname" onclick="cleanDiv()">X</a></div>
<div id="content" align="center"></div>
</section>
隨着單方面作風=「左:183px;頂部:111px;「onMouseDown =」dragStart(event,'results');「負責移動彈出屏幕(請參閱問題how to drag and drop a <div> across the page)
您有'JS/index.js' ......相對文件路徑所以它會被加載相對於包含js的頁面的位置而言。如果您使用Fiddler2或其他工具來觀看流量,那麼該文件將會出現404錯誤。 –
如果您使用的是Chrome或Firefox(或IE 9+),則瀏覽器內的開發工具會有一個網絡面板,該面板將顯示瀏覽器加載文件的錯誤。 Chrome:https://developers.google.com/chrome-developer-tools/docs/network#network_resource_details Firefox:https://developer.mozilla.org/en-US/docs/Tools/Network_Monitor IE:http:// blogs.msdn.com/b/ie/archive/2010/04/22/ie9-developer-tools-network-tab.aspx – HJ05
我使用Firefox的網絡監視器查看文件加載過程中可能出現的任何錯誤,並且我可以驗證所有文件加載正確,包括index.js。通過這種方式,我找不出爲什麼代碼不工作(在問題中添加了index.js的內容)。 –