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

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

jQuery在菜單上添加class .active

jQuery在菜單上添加class .active

Qyouu 2019-08-27 13:32:21
jQuery在菜單上添加class .active我有一個問題。我想在相關(guān)頁面打開時在項目菜單上添加“活動”類。菜單很簡單:<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/如果是這個我想添加第一類是link1的'a'元素。我正在嘗試很多解決方案,但沒有任何效果。
查看完整描述

3 回答

?
慕后森

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

點(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');
            }
        });});


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

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

對我來說更簡單的方法是:

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

因為我的鏈接轉(zhuǎn)到絕對網(wǎng)址,但如果您的鏈接是相對的,那么您可以使用:

 window.location**.pathname**


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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