gpt4 book ai didi

javascript - Codepen 六边形菜单故障排除帮助 : is my html file not properly linking to my javascript file?

转载 作者:可可西里 更新时间:2023-11-01 13:31:07 26 4
gpt4 key购买 nike

我是一个自学成才的编码员,如果有人能给我一些建议,让我知道如何让我从 codepen 下载的以下六边形动画菜单正常工作,我将不胜感激(链接:http://codepen.io/web-tiki/pen/WvNQpG)。现在我已经创建了 3 个文件:hexmenu.html、hexmenu.css 和 hexmenu.js 当然都在同一个文件夹中。但是当我将代码笔中的代码粘贴到每个文件中时,菜单似乎不起作用。链接到 js 和 css 的我的文件的头部如下:

<!DOCTYPE html>
<head>
<html lang="en">
<meta charset="UTF-8" />
<title>PASSION DEVELOPMENT PROJECT</title>
<link href="hexmenu.css" rel="stylesheet" type="text/css" />
<script src="hexmenu.js" ></script>
</head>

现在codepen中的所有html代码都在body标签中; hexmenu.css 中的 css 和 hexmenu.js 文件中的 javascript。每个文件都直接从 codepen 直接粘贴到不同的文件中,没有任何修改(这些文件中没有其他任何东西!),因此我有点困惑为什么这不起作用。有什么建议吗?! css 文档似乎可以正常工作,但我认为 javascript 端有问题。谁能让我知道我错过了什么?我什至创建了一个 jsfiddle 来检查代码本身是否正常工作并且一切运行顺利。然而,当我将文件放在桌面上并在 google chrome 和 firefox 上运行时,菜单没有打开......此时一个简单的修复将是天赐之物!我想作为一个新手,我一定在这里遗漏了一些明显的东西......

最佳答案

您需要在 javascript 中使用 window.onload。我检查以下 通过添加 window.onload 在我的本地代码,现在它像在 codepen 中一样工作魅力。
这里我将所有代码添加到同一个 HTML 页面中,您可以将 javascript 分开。

使用以下代码:

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script>
window.onload = function() {
var hexNav = document.getElementById('hexNav');

document.getElementById('menuBtn').onclick = function() {

var className = ' ' + hexNav.className + ' ';
if ( ~className.indexOf(' active ') ) {
hexNav.className = className.replace(' active ', ' ');
} else {
hexNav.className += ' active';
}
}
};
</script>
</head>
<body>
<nav id="hexNav">
<div id="menuBtn">
<svg viewbox="0 0 100 100">
<polygon points="50 2 7 26 7 74 50 98 93 74 93 26" fill="none" stroke-width="4" stroke="#585247" stroke-dasharray="0,0,300"/>
</svg>
<span></span>
</div>
<ul id="hex">
<li class="tr"><div class="clip"><a href="#" class="content">
<img src="https://farm8.staticflickr.com/7435/13629508935_62a5ddf8ec_c.jpg" alt="" />
<h2 class="title">Title</h2><p>Catch phrase</p>
</a></div></li>
<li class="tr"><div class="clip"><a href="#" class="content">
<img src="https://farm3.staticflickr.com/2827/10384422264_d9c7299146.jpg" alt="" />
<h2 class="title">Title</h2><p>Catch phrase</p>
</a></div></li>
<li class="tr"><div class="clip"><a href="#" class="content">
<img src="https://farm7.staticflickr.com/6083/6055581292_d94c2d90e3.jpg" alt="" />
<h2 class="title">Title</h2><p>Catch phrase</p>
</a></div></li>
<li class="tr"><div class="clip"><a href="#" class="content">
<img src="https://farm7.staticflickr.com/6092/6227418584_d5883b0948.jpg" alt="" />
<h2 class="title">Title</h2><p>Catch phrase</p>
</a></div></li>
<li class="tr"><div class="clip"><a href="#" class="content">
<img src="https://farm8.staticflickr.com/7187/6895047173_d4b1a0d798.jpg" alt="" />
<h2 class="title">Title</h2><p>Catch phrase</p>
</a></div></li>
<li class="tr"><div class="clip"><a href="#" class="content">
<img src="https://farm4.staticflickr.com/3766/12953056854_b8cdf14f21.jpg" alt="" />
<h2 class="title">Title</h2><p>Catch phrase</p>
</a></div></li>
</ul>
</nav>
</body>
</html>

希望对您有所帮助。 Fiddle link

关于javascript - Codepen 六边形菜单故障排除帮助 : is my html file not properly linking to my javascript file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29912243/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com