当我插入另一个脚本时,脚本停止工作

时间:2017-07-03 21:15:39

标签: javascript html

我有一个问题,现在正在杀了我,因为我无法理解为什么它不起作用。我在一个文件中创建了一个可用的脚本,但是当我将其复制/粘贴到我希望它工作的文件中时,它并不起作用。 这是我刚刚制作的剧本:

<html>
<body>
<p id="myElement"></p>
    <script>
        if (window.location.hash == "#hello") {
            document.getElementById("myElement").innerHTML = 'Vi har Hello';
        } else {
            document.getElementById("myElement").innerHTML = '';
        }
    </script>
</body>
</html>

这里有效,但是当我将它插入文件中时,它意味着它不会起作用。这是:

<html>
<head>
    <title>...</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link href="layout/styles/layout.css" rel="stylesheet" type="text/css" media="all">
    <link rel="shortcut icon" href="images/favicon.ico">
</head>
<body id="top">
    <div id="forside"></div>
    <div class="bgded overlay" style="background-image:url('images/stor_1.png');"> 
        <div id="pageintro" class="hoc clear">
            <li>
                <p></p><h2>Log ind</h2>
                <p id="myElement"></p>
                <script>
                    if (window.location.hash == "#hello") {
                    document.getElementById("myElement").innerHTML = 'Vi har Hello';
                    } else {
                    document.getElementById("myElement").innerHTML = '';
                    }
                </script>
                <div id="comments">
                    <font color="black">
                        <form action="proces.php" method="post">
                            <input class="form-control" type="text" id="name" size="22" name="username" placeholder="Brugernavn" required><br>
                            <input class="form-control" type="password" id="name" size="22" name="pass" placeholder="Kodeord" required><br> 
                            <input type="submit" name="submit" value="Log Ind!">
                        </form>
                    </font><br>
                    <a href="index.php"><i class="fa fa-chevron-left" aria-hidden="true"> Tilbage til forsiden</i></a>
                </div>
            </li>
        </div>
    </div>
<?php include_once "footer.php"; ?>
<a id="backtotop" href="#top"><i class="fa fa-chevron-up"></i></a>
    <script src="layout/scripts/jquery.min.js"></script>
    <script src="layout/scripts/jquery.mobilemenu.js"></script>
</body>
</html>

我插入的脚本的作用是检测#Hello是否在URL中,如果是,则必须显示一些文本......但它不会起作用。

谁知道我可能做错了什么?

2 个答案:

答案 0 :(得分:0)

您的代码只会在您的页面文档第一次加载时运行一次,如果您确实希望每次更改时检查哈希值,那么最好放入一个更改回调。 还请确保你的jQuery相对路径是正确的。

$(function() { // ensure you will execute script after document loaded

    $(window).on('hashchange', function() {   // put you code in side change callback.
        if (window.location.hash == "#hello") {
            document.getElementById("myElement").innerHTML = 'Vi har Hello';
        } else {
            document.getElementById("myElement").innerHTML = '';
        }
    });
});

<html>

<head>
    <title>...</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link href="layout/styles/layout.css" rel="stylesheet" type="text/css" media="all">
    <link rel="shortcut icon" href="images/favicon.ico">
</head>

<body id="top">
    <div id="forside"></div>
    <div class="bgded overlay" style="background-image:url('images/stor_1.png');">
        <div id="pageintro" class="hoc clear">
            <li>
                <p></p>
                <h2>Log ind</h2>
                <p id="myElement"></p>
                <script>
                </script>
                <div id="comments">
                    <font color="black">
                        <form action="proces.php" method="post">
                            <input class="form-control" type="text" id="name" size="22" name="username" placeholder="Brugernavn" required>
                            <br>
                            <input class="form-control" type="password" id="name" size="22" name="pass" placeholder="Kodeord" required>
                            <br>
                            <input type="submit" name="submit" value="Log Ind!">
                        </form>
                    </font>
                    <br>
                    <a href="index.php"><i class="fa fa-chevron-left" aria-hidden="true"> Tilbage til forsiden</i></a>
                </div>
            </li>
        </div>
    </div>
    <a id="backtotop" href="#top"><i class="fa fa-chevron-up"></i></a>
    <script src="layout/scripts/jquery.min.js"></script>
    <script src="layout/scripts/jquery.mobilemenu.js"></script>
    <script type="text/javascript">
    $(function() {
        $(window).on('hashchange', function() {
            if (window.location.hash == "#hello") {
                document.getElementById("myElement").innerHTML = 'Vi har Hello';
            } else {
                document.getElementById("myElement").innerHTML = '';
            }
        });
    });
    </script>
</body>

</html>

答案 1 :(得分:0)

尝试添加这些

<script src="layout/scripts/jquery.min.js"></script>
<script src="layout/scripts/jquery.mobilemenu.js"></script>

在头部的顶部