Javascript 当我插入另一个脚本时,脚本停止工作
我有一个问题,这是杀死我现在,因为我不明白为什么它不工作。我在一个文件中制作了一个脚本,但当我将它复制/粘贴到我希望它工作的文件中时,它就不工作了。 以下是我刚刚制作的脚本:Javascript 当我插入另一个脚本时,脚本停止工作,javascript,html,Javascript,Html,我有一个问题,这是杀死我现在,因为我不明白为什么它不工作。我在一个文件中制作了一个脚本,但当我将它复制/粘贴到我希望它工作的文件中时,它就不工作了。 以下是我刚刚制作的脚本: <html> <body> <p id="myElement"></p> <script> if (window.location.hash == "#hello") { document.getElementByI
<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>
if(window.location.hash==“你好”){
document.getElementById(“myElement”).innerHTML='Vi-har-Hello';
}否则{
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>
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>
...
Log ind
if(window.location.hash==“你好”){
document.getElementById(“myElement”).innerHTML='Vi-har-Hello';
}否则{
document.getElementById(“myElement”).innerHTML='';
}
我插入的脚本的作用是检测URL中是否有#Hello,如果有,它必须显示一些文本。。。但这行不通
有人知道我可能做错了什么吗?当您的页面文档第一次加载时,您的代码只会运行一次,如果您确实希望在每次更改哈希时都检查哈希,最好放入更改回调。 还请确保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 = '';
}
});
});
...
日志索引
$(函数(){
$(window).on('hashchange',function()){
if(window.location.hash==“你好”){
document.getElementById(“myElement”).innerHTML='Vi-har-Hello';
}否则{
document.getElementById(“myElement”).innerHTML='';
}
});
});
尝试添加这些
<script src="layout/scripts/jquery.min.js"></script>
<script src="layout/scripts/jquery.mobilemenu.js"></script>
在标题部分的顶部与PHP无关然后编辑标签@rtfm@JayBlanchard我在
script
tags中没有那种poweristype='
强制?在HTML5@rtfm中没有