![图片[1]-如何在Elementor中实现点击锚链接后自动关闭导航菜单-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/05/2024051714512069.png)
Setting up a directory to close after clicking an anchor link in Elementor (such as a collapsed menu or navigation menu) can be done using some custom code. Here are the specific steps:
Step 1: Edit Individual Post Templates
- log inWordPress DashboardThe
- Navigate to Elementortemplates > Saved templatesThe
![图片[2]-如何在Elementor中实现点击锚链接后自动关闭导航菜单-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/06/2024060515180233.png)
- Find your individual post template and click "Edit with Elementor".
Step 2: Adding HTML Elements
- In the template editor, find the section where you need to add the code, usually the end of the page.
- Drag and drop an HTML element into the template.
![图片[3]-如何在Elementor中实现点击锚链接后自动关闭导航菜单-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/06/2024060515230998.png)
Step 3: Paste the custom JavaScript code
In the HTML element, paste the following JavaScript code:
<script
document.addEventListener('DOMContentLoaded', function() {
var menuItems = document.querySelectorAll('.elementor-table-of-contents a'); // replace .elementor-table-of-contents with your catalog class
menuItems.forEach(function(menuItem) {
menuItem.addEventListener('click', function() {
var menuToggle = document.querySelector('.elementor-menu-toggle'); // replace .elementor-menu-toggle with your menu toggle button class
if (menuToggle) {
menuToggle.click(); // trigger the menu to close
}
}).
});
}).
</script
explanation
- class name matching::
.elementor-table-of-contents
is the class name of the directory, you need to replace it with the actual directory class name..elementor-menu-toggle
is the class name of the menu toggle button, you need to replace it with the actual menu toggle button class name.
- event listener::
document.addEventListener('DOMContentLoaded', function() { ... });
Make sure the code is executed after the page is fully loaded.menuItem.addEventListener('click', function() { ... });
Add a click event listener for each catalog link.
- Trigger menu off::
menuToggle.click();
Simulates clicking the menu toggle button, which closes the menu.
Step 4: Save and Publish
- Name your custom code (e.g. "Anchor Links Close Directory").
- Select "Entire site" as the scope of application for the code.
- Save and publish the code.
![图片[4]-如何在Elementor中实现点击锚链接后自动关闭导航菜单-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/06/2024060515274321.png)
Summary:
By adding custom JavaScript code to a single post template, you can implement an automatic closure of the directory after Elementor clicks on an anchor link. Key steps include editing the template, adding HTML elements, and pasting the code. Make sure to match the class name correctly to ensure the feature works properly. This setup not only optimizes the use of the navigation menu, but also enhances the overall user experience and interactivity of the site. Choosing this method helps keep the site professional and functional.
Link to this article:https://www.361sale.com/en/11104The article is copyrighted and must be reproduced with attribution.
No comments