微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我是初学者,面临着 Javascript 和 html 链接的问题在 HTML 中看不到 Javascript 的效果

如何解决我是初学者,面临着 Javascript 和 html 链接的问题在 HTML 中看不到 Javascript 的效果

我创建了一个 admin.html 文件,其中 h1 标签中有 div 类。我已经使用脚本连接了 admin.js 文件:src 但是当我在 Javascript 中更改颜色时无法看到效果,但是当我在 Chrome 中检查时相同的代码工作。感谢帮助。

admin.html

<!DOCTYPE html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <Meta http-equiv="X-UA-Compatible" content="IE=edge">
    <Meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>Admin</title>
    <link rel="stylesheet" href="style2.css">
    <script src="admin.js"></script>  
</head>
<body>
  <ul class="list"> 
    <li id="dashbord"style="font-size: 40px; padding-top: 8px; height: 50px;"><a  href="#dashbord">Dashboard</a></li>
    <br>
    <li><a class="active" href="#home">Home</a></li>
    <li><a href="#contact">Contact</a></li>
    <li><a href="#about">About</a></li>
  </ul>
  <div class="div" style="margin-left:25%;padding:1px 16px;height:1000px;">
    <h2>Fixed Full-height Side Nav</h2>
    <h3>Try to scroll this area,and see how the sidenav sticks to the page</h3>
    <p>Notice that this div element has a left margin of 25%. This is because the side navigation is set to 25% width. If you remove the margin,the sidenav will overlay/sit on top of this div.</p>
    <p>Also notice that we have set overflow:auto to sidenav. This will add a scrollbar when the sidenav is too long (for example if it has over 50 links inside of it).</p>
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
  </div>
  
</body>
</html>

admin.js

const q = document.querySelector('.div h2');
 q.style.color = 'red';

解决方法

原因是在读取 HTML 文件和构建 DOM 之前对脚本进行了解析和评估。 DOM 元素此时不存在。

script 标签移到 body 的末尾。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>Admin</title>
    <link rel="stylesheet" href="style2.css">
</head>
<body>
  <ul class="list"> 
    <li id="dashbord"style="font-size: 40px; padding-top: 8px; height: 50px;"><a  href="#dashbord">Dashboard</a></li>
    <br>
    <li><a class="active" href="#home">Home</a></li>
    <li><a href="#contact">Contact</a></li>
    <li><a href="#about">About</a></li>
  </ul>
  <div class="div" style="margin-left:25%;padding:1px 16px;height:1000px;">
    <h2>Fixed Full-height Side Nav</h2>
    <h3>Try to scroll this area,and see how the sidenav sticks to the page</h3>
    <p>Notice that this div element has a left margin of 25%. This is because the side navigation is set to 25% width. If you remove the margin,the sidenav will overlay/sit on top of this div.</p>
    <p>Also notice that we have set overflow:auto to sidenav. This will add a scrollbar when the sidenav is too long (for example if it has over 50 links inside of it).</p>
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
  </div>
  
  <script src="admin.js"></script>  
</body>
</html>

某些框架(如 Webpack)将 script 标记添加到头部并添加 defer,但我不确定这是否总是有效。根据 https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script,它应该始终有效。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>Admin</title>
    <link rel="stylesheet" href="style2.css">
    <script src="admin.js" defer></script>  
</head>
<body>
  <ul class="list"> 
    <li id="dashbord"style="font-size: 40px; padding-top: 8px; height: 50px;"><a  href="#dashbord">Dashboard</a></li>
    <br>
    <li><a class="active" href="#home">Home</a></li>
    <li><a href="#contact">Contact</a></li>
    <li><a href="#about">About</a></li>
  </ul>
  <div class="div" style="margin-left:25%;padding:1px 16px;height:1000px;">
    <h2>Fixed Full-height Side Nav</h2>
    <h3>Try to scroll this area,the sidenav will overlay/sit on top of this div.</p>
    <p>Also notice that we have set overflow:auto to sidenav. This will add a scrollbar when the sidenav is too long (for example if it has over 50 links inside of it).</p>
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
    <p>Some text..</p>
  </div>
  
</body>
</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。