我正在根据 URL 测试一个东西。如果 URL 以 logout.jsp 结尾,则应显示注销消息
如果 URL 以 index.jsp 结尾,则不应显示该消息。为此我使用了
document.getElementById("id").hidden = value
。但这不起作用。事实上该函数没有被调用。
我不知道问题出在哪里。
HTML 片段
<table id="LogoutMessage" onload="urlCheck()">
<tr>
<td>
<h2>You are successfully logged out !</h2>
</td>
</tr>
</table>
javascript函数
<script type="text/javascript">
function urlCheck() {
alert("in the function urlCheck");
if(document.URL.endsWith() = "logout.jsp")
document.getElementById("LogoutMessage").hidden = false;
else if(document.URL.endsWith() = "index.jsp")
document.getElementById("LogoutMessage").hidden = true;
}
</script>
整个页面 在 JSP 中
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PhotoArtWork</title>
<jsp:include page="reusable/stylesheets.html" />
<script type="text/javascript" src="js/modernizr-1.5.min.js"></script>
</head>
<body>
<jsp:include page="reusable/header.html" />
<!-- begin content --><div id="site_content">
<table id="LogoutMessage" onload="urlCheck()">
<tr>
<td>
<h2>You are successfully logged out !</h2>
</td>
</tr>
</table>
<ul class="slideshow">
<li class="show"><img width="950" height="450" src="images/home_1.jpg" alt=""You can put a caption for your image right here""></li>
<li><img width="950" height="450" src="images/home_2.jpg" alt=""You can put a description of the image here if you like, or anything else if you want.""></li>
<li><img width="950" height="450" src="images/home_3.jpg" alt=""You can put a description of the image here if you like, or anything else if you want.""></li>
</ul>
</div>
<!-- end content -->
<script type="text/javascript">
function urlCheck() {
alert("in the function urlCheck");
if(document.URL.endsWith() = "logout.jsp")
document.getElementById("LogoutMessage").hidden = false;
else if(document.URL.endsWith() = "index.jsp")
document.getElementById("LogoutMessage").hidden = true;
}
</script>
<jsp:include page="reusable/footer.html" />
</body>
</html>
问题是什么?
最佳答案
没有 .hidden
属性。它是元素样式的一部分,您将使用可见性。
document.getElementById("LogoutMessage").style.visibility = "hidden"; // or "visible"
如果我不得不猜测,您想使用显示
而不是隐藏
。
document.getElementById("LogoutMessage").style.display = "none"; // or "block"
关于javascript - 为什么该函数没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10268391/