第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

jQuery在菜單上添加class .active

jQuery在菜單上添加class .active

Qyouu 2019-08-27 13:32:21
jQuery在菜單上添加class .active我有一個(gè)問題。我想在相關(guān)頁面打開時(shí)在項(xiàng)目菜單上添加“活動(dòng)”類。菜單很簡單:<div class="menu"><ul><li><a href="~/link1/">LINK 1</a><li><a href="~/link2/">LINK 2</a><li><a href="~/link3/">LINK 3</a></ul></div>在jQuery中,我需要檢查網(wǎng)址是否為www.xyz.com/other/link1/如果是這個(gè)我想添加第一類是link1的'a'元素。我正在嘗試很多解決方案,但沒有任何效果。
查看完整描述

3 回答

?
慕后森

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

點(diǎn)擊這里查看jsFiddle中的解決方案

你需要的是你需要獲得所提到的window.location.pathname,然后從中創(chuàng)建regexp并根據(jù)導(dǎo)航hrefs進(jìn)行測試。

$(function(){

    var url = window.location.pathname, 
        urlRegExp = new RegExp(url.replace(/\/$/,'') + "$"); // create regexp to match current url pathname and remove trailing slash if present as it could collide with the link in navigation in case trailing slash wasn't present there
        // now grab every link from the navigation
        $('.menu a').each(function(){
            // and test its normalized href against the url pathname regexp
            if(urlRegExp.test(this.href.replace(/\/$/,''))){
                $(this).addClass('active');
            }
        });});


查看完整回答
反對(duì) 回復(fù) 2019-08-27
?
楊__羊羊

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊

對(duì)我來說更簡單的方法是:

var activeurl = window.location;$('a[href="'+activeurl+'"]').parent('li').addClass('active');

因?yàn)槲业逆溄愚D(zhuǎn)到絕對(duì)網(wǎng)址,但如果您的鏈接是相對(duì)的,那么您可以使用:

 window.location**.pathname**


查看完整回答
反對(duì) 回復(fù) 2019-08-27
  • 3 回答
  • 0 關(guān)注
  • 908 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)