Hongmu Notes
Home Language Notes jQuery uses cookies to add special class attributes to the navigation column
Language Notes JavaScript

jQuery uses cookies to add special class attributes to the navigation column

jQuery uses cookies to add special class attributes to the navigation column

When a user clicks on an <a> tag within a specified container, the link associated with that <a> tag is stored in a cookie. Upon page redirection, if the cookie's stored link matches the URL of the <a> tag, an attribute is added to that <a> tag; only one such cookie is stored. When the user clicks on another <a> tag within the same container, the link is updated directly.

$(document).ready(function() {
// 获取box里的所有a标签
var links = $('#selectBox a');

// 点击a标签时设置cookie并加上属性
links.click(function() {
    // 设置cookie
    var link = $(this).attr('href');
    document.cookie = "link=" + link + "; path=/";
    return true;
});

// 检查cookie并更新a标签链接
var link = getCookie("link");
if (link) {
    // alert(link)
    links.each(function() {
    if ($(this).attr('href') == link) {
        $(this).addClass('current');
    } 
    });
}
});

// 获取cookie
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
    var cookies = document.cookie.split(';');
    for (var i = 0; i < cookies.length; i++) {
    var cookie = jQuery.trim(cookies[i]);
    if (cookie.substring(0, name.length + 1) == (name + '=')) {
        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
        break;
    }
    }
}
return cookieValue;
}

Common Navigation – Select

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

js move the mouse up to modify the style attribute

js move the mouse up to modify the style attribute Language Notes JavaScript

When the mouse hovers over the element, the color changes to red; when the mouse leaves the element, the color changes to black. To retrieve an element by its ID: `document.getElementById()`; Event handler for mouse hover: `onmouseover`; Event handler for mouse leave: `onmouseout`.<!DOCTYPE html><html lang= "en">...
👁 130

Recommended reading

Cake & Bread Food Website Template 0123

Cake & Bread Food Website Template 0123 Practical Collection Yiyou template

This EYO CMS template is ideal for the cake and bread industry, featuring a warm and inviting design style perfect for showcasing baked goods, brand stories, and franchise information. It helps baking brands attract consumers and franchisees online while enhancing their brand awareness. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EYO CMS | EYO CMS (Eyo...
👁 63
(Adaptive mobile version) PBootCMS template for news and blog websites – Download HTML5 responsive news blog website source code – 0517

(Adaptive mobile version) PBootCMS template for news and blog websites – Download HTML5 responsive news blog website source code – 0517 Practical Collection pbootcms Template

A responsive PbootCMS website template designed for news and blog content, compatible with both PC and WAP devices. It features a modern information-flow design, making it ideal for aggregating industry news, in-depth reports, and commentary articles. This template helps news websites attract online readers and build media influence. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 39
Responsive high-end home custom website template 0926

Responsive high-end home custom website template 0926 Practical Collection Yiyou template

This set of eyoucms responsive templates is suitable for the high-end home customization industry. The design style is luxurious and modern, and can display the whole house custom home, design cases, brand stories and service processes. It helps high-end home furnishing brands attract owners online and enhance their brand image. Template display Installation instructions Website backend: /login.php Account: admin Password: admin Related articles Yiyou CMS installation FAQ summary Yiyou CM...
👁 36
After installing Redis on WordPress, setting a password prevented Redis from connecting; as a result, Redis could not be accessed: `SELECT` Failed: NOAUTH – Authentication required.

After installing Redis on WordPress, setting a password prevented Redis from connecting; as a result, Redis could not be accessed: `SELECT` Failed: NOAUTH – Authentication required. Program Notes Summary of pitfalls wordpress

This is the most widely used article online – it can solve 80% of Redis-related issues! What should you do if the Redis Object Cache plugin fails to connect after you have changed the Redis password in WordPress? Of course, the remaining 20% of cases require making adjustments directly in the wp-config.php file! Solution: Previously, I added Redis-related code to the object-cache.php file of the Redis plugin...
👁 605
Intellectual Property Financial Accounting Website Template 0124

Intellectual Property Financial Accounting Website Template 0124 Practical Collection Yiyou template

An EyouCMS website template designed for the intellectual property and financial accounting industries. Its professional and sophisticated design effectively showcases intellectual property services, financial accounting services, and success stories. This template helps professional service providers attract corporate clients online and expand their business reach. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 45
(Adaptive mobile version) Responsive art exam preparation school website template – PBootCMS; Download HTML5 art training institution website source code – 0518

(Adaptive mobile version) Responsive art exam preparation school website template – PBootCMS; Download HTML5 art training institution website source code – 0518 Practical Collection pbootcms Template

A responsive PbootCMS website template designed for art exam preparation schools and art training institutions, compatible with both PC and WAP devices. Its professional artistic design is ideal for showcasing art exam courses, faculty profiles, and student works. This template helps art training institutions attract art exam candidates online and enhance their brand visibility. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s...
👁 58