Konami Code触发后链接不显示问题求助(附代码)
Hey there! Let's figure out why your Konami Code-triggered link isn't showing up. The alert works because that part of the code runs fine, but the link creation has a couple of critical missing pieces. Let's break it down:
What's going wrong in your current code
- Your jQuery code to create the link is incomplete — you started
$("<a href='../controlleur/pag...")but never finished closing the tag or adding it to the DOM. Just creating the jQuery object doesn't make it appear on the page. - You're not clearing the
kkeysarray after the code triggers, so if someone keeps pressing keys, it might trigger repeatedly unexpectedly. - Also, double-check that you've included the jQuery library on your page — if
$isn't defined, that line will fail silently (though the alert still works since it's vanilla JS).
Fixed code (jQuery version)
if (window.addEventListener) { var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65"; window.addEventListener("keydown", function(e) { kkeys.push(e.keyCode); // Check if the Konami sequence is matched if (kkeys.toString().indexOf(konami) >= 0) { alert("Ajout d'animes à la base de donnée: ACTIVÉ"); // Create the complete link element and add it to the DOM var secretLink = $("<a href='../controlleur/your-full-page-path.html' target='_blank'>Ajouter des animes</a>"); // Append it to a specific element (body, or a container with an ID like #secret-container) $("body").append(secretLink); // Clear the key array to prevent repeated triggers kkeys = []; } }); }
Fixed code (Vanilla JS version, no jQuery needed)
If you don't want to rely on jQuery, here's a vanilla JS alternative that works without external libraries:
if (window.addEventListener) { var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65"; window.addEventListener("keydown", function(e) { kkeys.push(e.keyCode); if (kkeys.toString().indexOf(konami) >= 0) { alert("Ajout d'animes à la base de donnée: ACTIVÉ"); // Create the link element from scratch var secretLink = document.createElement("a"); secretLink.href = "../controlleur/your-full-page-path.html"; secretLink.textContent = "Ajouter des animes"; secretLink.target = "_blank"; // Optional: opens link in new tab // Add it to the page (append to body or a specific container) document.body.appendChild(secretLink); // Reset the key array to avoid repeated triggers kkeys = []; } }); }
Key fixes explained
- Complete the link element: Make sure the HTML tag is properly closed (for jQuery) or use native methods to set all attributes (for vanilla JS).
- Append to the DOM: You have to explicitly add the new element to the page using
append()(jQuery) orappendChild()(vanilla JS) — otherwise, it only exists in memory and won't be visible. - Reset the key array: Clearing
kkeysafter the trigger ensures the code only runs once per full Konami sequence, not every time the sequence appears in the key press history. - Verify jQuery setup: If you stick with the jQuery version, ensure you've included the jQuery library in your HTML before your custom script (e.g.,
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>).
内容的提问来源于stack exchange,提问作者Methraen
相关产品推荐
相关产品推荐

