如何实现点击按钮加载course.html页面的JavaScript代码?
Hey there! Let's get your button working correctly to load course.html when clicked. I spotted a couple of small issues in your current code snippet—here's the fully corrected version, plus breakdowns of what was fixed:
Corrected Full Code
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <script type="text/javascript"> function loadCourse(){ // Navigate to course.html when the function runs window.location.href = "course.html"; } </script> </head> <body> <input type="button" id="myButton" value="Find your course now!"> <script type="text/javascript"> // Bind the loadCourse function to the button's click event document.getElementById("myButton").onclick = loadCourse; </script> </body>
Key Fixes & Explanations
- Fixed the
onclickbinding: Your original code assigned a string"loadCourse()"toonclick, which won't trigger the function properly. Instead, we directly assign the function referenceloadCourse(no quotes, no parentheses—we want the button to run the function when clicked, not immediately when the page loads). - Added page navigation logic to
loadCourse: The function now useswindow.location.href = "course.html";to redirect the current browser tab tocourse.html. If you'd prefer to open the page in a new tab instead, swap that line withwindow.open("course.html"). - Corrected a meta tag typo: Changed
width=divice-widthtowidth=device-width—this fixes a small mobile viewport issue that could have broken responsive layout behavior.
内容的提问来源于stack exchange,提问作者K Deven
相关产品推荐
相关产品推荐

