0

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="&quot;You can put a caption for your image right here&quot;"></li>
    <li><img width="950" height="450" src="images/home_2.jpg" alt="&quot;You can put a description of the image here if you like, or anything else if you want.&quot;"></li>
    <li><img width="950" height="450" src="images/home_3.jpg" alt="&quot;You can put a description of the image here if you like, or anything else if you want.&quot;"></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>

何が問題ですか ?

4

4 に答える 4

1

コードを呼び出すには、次のいずれかを行います。

<body onload="urlCheck()">

または、スクリプトをbodyタグの最後に配置し、関数の内部コードのみを追加します。

于 2012-04-22T13:54:12.067 に答える
1

.hidden属性はありません。これは要素のスタイルの一部であり、可視性を使用します。

document.getElementById("LogoutMessage").style.visibility = "hidden"; // or "visible"

displayそして、私が推測しなければならなかった場合、あなたはand notを使用したいと思いますhidden

document.getElementById("LogoutMessage").style.display = "none"; // or "block"
于 2012-04-22T13:41:29.990 に答える
0

jQueryを使用できます。含む

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

と使用

$('#LogoutMessage').hide();

メッセージを非表示にするため

$('#LogoutMessage').show();

メッセージを表示するため

関数を呼び出すには

$(document).ready(function(){
    urlCheck();
})
function urlCheck() {
    var url = document.URL;
    var urlArr = url.split('/')
    if($.inArray('logout.jsp', urlArr))
    {
        $('#LogoutMessage').show();
    }
    if($.inArray('index.jsp', urlArr))
    {
        $('#LogoutMessage').hide();
    }
}

上記を試してください。

于 2012-04-22T15:08:03.773 に答える
0

Javascript にはネイティブのendsWith. したがって、最初に作成する必要があります

String.prototype.endsWith = function(suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
};

また、ありませんstyle.hidden propertystyle.display代わりに使用してください

正しい関数urlCheckは次のようになります

function urlCheck() {
    alert("in the function urlCheck");
    if(document.URL.endsWith("logout.jsp"))
        document.getElementById("LogoutMessage").style.display= 'block';
    else if(document.URL.endsWith("index.jsp"))
        document.getElementById("LogoutMessage").style.display= 'none';
}

ああ、Mic が言うように、関数 onload をこのように呼び出してください。

window.onload = function() { urlCheck(); }
于 2012-04-22T13:58:40.810 に答える