Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript是否依赖于声明的位置?javascript程序上的奇怪行为_Javascript_Html - Fatal编程技术网

javascript是否依赖于声明的位置?javascript程序上的奇怪行为

javascript是否依赖于声明的位置?javascript程序上的奇怪行为,javascript,html,Javascript,Html,我刚刚完成了一个基本的javasript程序,我所做的只是将javascript声明从body之后移动到body之前,而javscript只会产生错误。据我所知,javascript不会因声明的位置而改变。如果是,为什么会有这种行为 <!DOCTYPE html> <html> <head> <style> /* The Modal (background) */ .modal { display: none; /* Hidden by d

我刚刚完成了一个基本的javasript程序,我所做的只是将javascript声明从body之后移动到body之前,而javscript只会产生错误。据我所知,javascript不会因声明的位置而改变。如果是,为什么会有这种行为

<!DOCTYPE html>
<html>
<head>
<style>
/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
    position: relative;
    background-color: #fefefe;
    margin: auto;
    padding: 0;
    border: 1px solid #888;
    width: 80%;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
    -webkit-animation-name: animatetop;
    -webkit-animation-duration: 0.4s;
    animation-name: animatetop;
    animation-duration: 0.4s
}

/* Add Animation */
@-webkit-keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

@keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

/* The Close Button */
.close {
    color: white;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

.modal-header {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}

.modal-body {padding: 2px 16px;}

.modal-footer {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}
</style>
</head>
<body>

<h2>Animated Modal with Header and Footer</h2>

<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">
      <span class="close">×</span>
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>

</div>

<script>
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal
btn.onclick = function() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
</script>

</body>
</html>
我做的简单代码更改。。。
浏览器访问Javascript时会运行Javascript。当您将其移动到元素声明之前时,您的JS将尝试访问尚未在页面中创建的DOM节点。

浏览器访问它时,会运行Javascript。当您将其移动到元素声明之前时,您的JS正在尝试访问尚未在页面中创建的DOM节点。

它是相关的。 所有选择器都将失败,因为它们试图选择的HTML元素在代码执行时不存在

当然,监听器也是如此,除了窗口监听器,因为当页面开始加载时,窗口对象就存在了

请看区别:

alertdocument.getElementById'e'; 它是独立的。 所有选择器都将失败,因为它们试图选择的HTML元素在代码执行时不存在

当然,监听器也是如此,除了窗口监听器,因为当页面开始加载时,窗口对象就存在了

请看区别:

alertdocument.getElementById'e';
是的,这是完全不同的。似乎涵盖得很好。您给出的第二个示例不是有效的HTML-脚本标记必须进入头部或身体内部。据我所知,javascript不会因声明的位置而改变,只要我们讨论的是相同的范围,这是正确的,不同的范围会有不同,运行时刻也会有很大的不同。@Dellirium运行时刻会有很大的不同。。是的,我现在明白那是什么意思了。谢谢,这是完全不同的。似乎涵盖得很好。您给出的第二个示例不是有效的HTML-脚本标记必须进入头部或身体内部。据我所知,javascript不会因声明的位置而改变,只要我们讨论的是相同的范围,这是正确的,不同的范围会有不同,运行时刻也会有很大的不同。@Dellirium运行时刻会有很大的不同。。是的,我现在明白那是什么意思了。谢谢汉克斯。。。。我是编程新手,这个答案真的让我在未来面临最大的困惑之一。我之前写的所有脚本都是在页面加载后调用/执行的,但是这个脚本是在脚本本身加载时执行的。这有很大的不同。不客气,我很高兴我能帮助你。是的,你需要小心这样的东西,即使是更有经验的开发人员偶尔也会犯同样的错误,忽略它。与此相关的一件有趣的事情是函数提升。有机会的时候用谷歌搜索一下。谢谢。。。。我是编程新手,这个答案真的让我在未来面临最大的困惑之一。我之前写的所有脚本都是在页面加载后调用/执行的,但是这个脚本是在脚本本身加载时执行的。这有很大的不同。不客气,我很高兴我能帮助你。是的,你需要小心这样的东西,即使是更有经验的开发人员偶尔也会犯同样的错误,忽略它。与此相关的一件有趣的事情是函数提升。有机会的时候用谷歌搜索一下。
<!DOCTYPE html>
<html>
<head>
<style>
/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
    position: relative;
    background-color: #fefefe;
    margin: auto;
    padding: 0;
    border: 1px solid #888;
    width: 80%;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
    -webkit-animation-name: animatetop;
    -webkit-animation-duration: 0.4s;
    animation-name: animatetop;
    animation-duration: 0.4s
}

/* Add Animation */
@-webkit-keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

@keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

/* The Close Button */
.close {
    color: white;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

.modal-header {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}

.modal-body {padding: 2px 16px;}

.modal-footer {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}
</style>
</head>
<script>
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal
btn.onclick = function() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
</script>
<body>

<h2>Animated Modal with Header and Footer</h2>

<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">
      <span class="close">×</span>
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>

</div>

</body>
</html>