簡體   English   中英

我的任何瀏覽器均無法使用JavaScript

[英]JavaScript is Not Working in any of my Browsers

我只是在學習JavaScript,由於某種原因,這個簡單的示例( 第一章的示例1)在我的任何瀏覽器(FireFox,IE和Chrome)中都無法正常工作(喬恩·達克特的JavaScript和Jquery書)。 我確保打開了JavaScript。 我還將noscript標記放入index.html文件中,以確保啟用了JavaScript。 我的jsFiddle完美運行,但是在我的瀏覽器中什么都沒有。 這是我的jsFiddle,其中包含我在Atom編輯器中擁有的所有內容。 我的鏈接是正確的,因為我復制了完整路徑,然后將其粘貼到src中。 然后刪除所有C:\\ users .......,直到進入所有文件的文件夾,最終顯示jsFiddle中的內容。

的HTML:

<!doctype html>
<html lang="en">
<head>
<meta charset="etf-8" />
<title>Greeting JavaScript</title>
<link rel="stylesheet" href="css/styles.css">
<script src="scripts/greeting.js"></script>
</head>
<body>
<noscript>Your Browser Does Not Support JavaScript.  Please Enable It.</noscript>
<section>
<div id="header">
  <h1>Constructive &amp; Co.</h1>
</div>
<h3 id="greeting"></h3>
<p>For all orders and inquiries please call <em>555-3344</em></p>
</section>
</body>
</html>

javascript:

var today = new Date();
var hourNow = today.getHours();
var greeting;

if (hourNow > 18) {
 greeting = 'Good evening!';
} else if (hourNow > 12) {
 greeting = 'Good afternoon!';
} else if (hourNow > 0) {
 greeting = 'Good morning!';
} else {
 greeting = 'Welcome!';
}

document.getElementById("greeting").innerHTML = greeting;

CSS:

@import url(http://fonts.googleapis.com/css?family=Open+Sans:800italic);

body {
font-family: "Courier New", Courier, monospace;
/*background: url("../images/constructive-backdrop.jpg") no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;*/
background: #a18957;
margin: 0px;
padding: 0px;
text-align: center;
}
section {
margin-top: 20px;
margin-left: 20px;
height: 500px;
width: 400px;
background: #eee;
border: 1px solid #292929;
}
#header {
height: 200px;
margin: 10px;
background: rgba(227, 192, 186, 0.78);
}
h1 {
margin: 0px;
position: relative;
top: 45%;
}
h3 {
height: 100px;
margin: 10px;
background: red;
}
p {
margin: 10px;
height: 100px;
}

謝謝

您要在html的末尾添加script標簽。 執行時,元素greeting不存在。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Greeting JavaScript</title>
  <link rel="stylesheet" href="css/styles.css">
</head>
<body>
  <noscript>Your Browser Does Not Support JavaScript.  Please Enable It.</noscript>
  <section>
    <div id="header">
      <h1>Constructive &amp; Co.</h1>
    </div>
    <h3 id="greeting"></h3>
    <p>For all orders and inquiries please call <em>555-3344</em></p>
  </section>
  <script src="scripts/greeting.js"></script>
</body>
</html>

或者您將其放在頭部,然后將腳本包裝到

addEventListener("readystatechange", function () {
  if (document.readyState === "interactive") {

    var today = new Date();
    var hourNow = today.getHours();
    var greeting;

    if (hourNow > 18) {
      greeting = 'Good evening!';
    } else if (hourNow > 12) {
      greeting = 'Good afternoon!';
    } else if (hourNow > 0) {
      greeting = 'Good morning!';
    } else {
      greeting = 'Welcome!';
    }

    document.getElementById("greeting").innerHTML = greeting;

  }
}, false);

使用document.getElementById而不是document.write真是太好了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM