求助:Foundation Framework Off-Canvas Menu无法打开及JS报错
Let’s break this down clearly— that Foundation JS error is definitely the reason your off-canvas menu won’t trigger, and it’s a common hiccup with Foundation 6 in the JointsWP theme. Here’s what’s going on and how to fix it:
What the Error Means
The TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element' tells us Foundation’s JavaScript is trying to run the getComputedStyle() method on something that isn’t a valid DOM element. For off-canvas menus, this almost always stems from one of these issues:
- Missing or malformed off-canvas menu markup
- Foundation initializing before the menu element loads in the DOM
- A mismatch in the data attributes that link the trigger button to the menu
Step-by-Step Fixes
Double-check your off-canvas markup structure
JointsWP relies on Foundation’s off-canvas component, so make sure your HTML follows the required pattern exactly:<!-- Off-Canvas Menu Container --> <div class="off-canvas position-left" id="offCanvas" data-off-canvas> <!-- Your menu links/content go here --> </div> <!-- Menu Trigger Button --> <button type="button" class="menu-icon" data-toggle="offCanvas"> <span></span> <!-- For hamburger icon styling --> </button> <!-- Main Content Wrapper (Required!) --> <div class="off-canvas-content" data-off-canvas-content> <!-- Your site's main content lives here --> </div>Critical note: The value in
data-toggleon the button must match theidof your off-canvas div. If these don’t line up, Foundation can’t connect the trigger to the menu.Ensure Foundation initializes after the DOM is ready
In JointsWP, Foundation is typically initialized inassets/js/scripts.js. Look for code like this:jQuery(document).ready(function($) { $(document).foundation(); });If the
$(document).foundation();line is running outside of thedocument.readywrapper, Foundation might try to access the menu element before it exists in the DOM. Add the wrapper if it’s missing— this guarantees all elements are loaded before Foundation starts up.Rule out script conflicts
Other WordPress plugins or custom JS can interfere with Foundation. Try:- Disabling non-essential plugins one by one to see if the menu starts working
- Temporarily removing any custom JavaScript you’ve added to
scripts.jsor other theme files to isolate the issue
Update Foundation to a newer (compatible) version
Your error referencesfoundation.min.js?ver=6.0— Foundation 6.0 is outdated, and later releases fixed many bugs related to DOM element detection. You can update the Foundation files in your theme’sassets/js/vendorfolder (just make sure to back up the original files first).Debug with unminified Foundation JS (optional)
If you want to pinpoint the exact component causing the issue, swap the minifiedfoundation.min.jswith the unminifiedfoundation.jstemporarily. The console error will now point to a specific line in the code (likely the OffCanvas component), which can help you confirm if the problem is with your markup or initialization logic.
Once you resolve the root cause of that TypeError, your off-canvas menu should trigger without issues.
内容的提问来源于stack exchange,提问作者Brett

