简体   繁体   English

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

[英]Script stops working when I insert into another script

I have a problem that is killing me right now because I can't see why it isn't working. 我有一个正在杀死我的问题,因为我不明白为什么它不起作用。 I have made a script in one file that works, but when I copy/paste it into the file that I want it to work in, it doesn't work. 我已经在一个可以使用的文件中制作了一个脚本,但是当我将其复制/粘贴到我希望其使用的文件中时,它将不起作用。 Here's the script I just made: 这是我刚刚编写的脚本:

<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>

Here it works, but when I insert it into the file that it was meant for it won't work. 在这里它可以工作,但是当我将它插入文件时,它就不起作用了。 Here it is: 这里是:

<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>

What the script, I am inserting, does is to detect if #Hello is in the URL and if it is, it has to display some text... But it won't work. 我要插入的脚本的作用是检测URL中是否包含#Hello,如果存在,则必须显示一些文本...但是它将无法工作。

Anybody who has an idea about what I could be doing wrong? 有人对我可能做错了什么有想法吗?

your code will only run once when your page document first time load, if you do want to check the hash every time its changed, it is better to put in a change callback. 您的代码仅在首次加载页面文档时运行一次,如果您确实想在每次更改哈希值时对其进行检查,则最好放置一个更改回调。 please also be sure you jQuery relative path is right. 请确保您的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> 

try adding these 尝试添加这些

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

at the top in the head section 在头部的顶部

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

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