输入框关联Keydown函数失效?登录页面彩蛋开发遇问题
Hey there! Let's work through why your input's keydown function isn't firing for that cool xkcd comic Easter egg. Based on the code snippets you shared, here are the most likely issues and fixes:
1. jQuery Isn't Loaded (or Loaded Too Late)
Your getFun() function uses $.ajax, which relies on jQuery. If jQuery isn't properly loaded before your custom script, none of the jQuery-dependent code (including keydown event binding) will work.
Fix:
- Make sure jQuery is included in your page before your Easter egg script. You can host it locally and add it like this in the
<head>or right before your closing</body>tag:<script src="/path/to/your/local/jquery.min.js"></script>
2. Event Binding Happens Before the Input Exists in the DOM
If your script runs before the <input id="Z"> element is rendered, jQuery can't find it to bind the keydown event.
Fix:
Wrap your keydown binding inside a document.ready handler to ensure it runs only after the DOM is fully loaded:
$(document).ready(function() { // Bind keydown to the input $('#Z').on('keydown', function(e) { // Example: Trigger the Easter egg when Ctrl+Enter is pressed if (e.ctrlKey && e.key === 'Enter') { e.preventDefault(); // Prevent form submission if needed getFun(); } }); });
Alternatively, place your custom script at the very bottom of the page, right before </body>, so it runs after the input element is created.
3. The Keydown Event Is Being Blocked
Another script on the page might be intercepting the keydown event and calling e.preventDefault() or return false, which stops your handler from running.
Test & Fix:
Add a console log to your keydown handler to check if it's even triggering:
$('#Z').on('keydown', function(e) { console.log('Key pressed:', e.key); // Check browser console for this message // Your existing trigger logic here });
If you don't see the log, scan other scripts on the page for code that modifies keydown behavior on inputs or the document.
4. The getFun() Function Is Out of Scope
If getFun() is defined inside a local scope (like another function or closure), your keydown handler won't be able to call it.
Fix:
Ensure getFun() is defined in the global scope (outside any other functions) or make sure your keydown handler has access to it. Here's a complete, scoped-safe version of your function:
function getFun() { var max = 1971; var rando = Math.floor(Math.random() * max); var getFunURL = 'https://xkcd.now.sh/' + rando; $.ajax({ url: getFunURL, method: 'GET', success: function(comicData) { // Render the comic on the page (adjust this to fit your layout) var comicHTML = ` <div style="position: fixed; top: 20px; left: 20px; z-index: 9999; background: white; padding: 10px; border: 1px solid #ccc;"> <img src="${comicData.img}" alt="${comicData.alt}" style="max-width: 400px;" /> </div> `; $('body').append(comicHTML); }, error: function() { console.log('Failed to load the comic—try again!'); } }); }
Start with checking jQuery and DOM readiness first—those are the most common culprits for this kind of issue. Let me know if you hit any other snags!
内容的提问来源于stack exchange,提问作者user9105429

