by Razor12345 28/2/2024, 20:15
AP - Modules - Javascript code management - Create new JavascriptTitle: any
Placement: In all the pages
Code: - Code:
$(function() {
const menuButton = document.querySelector('#main-menu-btn');
menuButton.addEventListener('click', function() {
const extraLink = document.querySelector('.extra_link');
const isOpenMenu = document.querySelector('#main-menu div.divider + ul');
if (isOpenMenu && !extraLink) {
isOpenMenu.insertAdjacentHTML('beforeend', '<li class="extra_link"><a href="/"><span>Extra link<span></a></li>');
}
});
});
In this code:
- Code:
const menuButton = document.querySelector('#main-menu-btn'); |
- button that opens the navigation menu
- Code:
const extraLink = document.querySelector('.extra_link');
|
- code that looks for additional links in the navigation menu. If these links are found, the code will not allow these links to be inserted into the navigation menu again.
- Code:
const isOpenMenu = document.querySelector('#main-menu div.divider + ul'); |
- the place where the navigation buttons are inserted
- Code:
isOpenMenu.insertAdjacentHTML('beforeend', '<li class="extra_link"><a href="/"><span>Extra link<span></a></li>'); |
- code that inserts the HTML markup of our links. If you need to insert more than one link in the navigation menu, use the following code as an example:
- Code:
isOpenMenu.insertAdjacentHTML('beforeend', '<li class="extra_link"><a href="/"><span>Extra link<span></a></li><li class="extra_link"><a href="/"><span>Extra link 2<span></a></li>'); |
Using this HTML markup of link will preserve the default link style in the navigation menu.
Result: