2017-01-09 21 views
-6
var path = window.location.pathname; // Returns path only 
var element = jq_menu("#topMenu a[href='" + path + "']"); 
element.parent().closest('li').addClass('active'); 

我使用此代碼來選擇活動菜單。如何在選擇器條件中使用低功耗條件在jQuery中

我想href值轉換爲lowerCase在第2行

我怎樣才能做到這一點?

+0

'path'是一個字符串,所以...'path.toLowerCase()'O.o – Andreas

+0

你試過了嗎'path.toLowerCase()'? – Rajesh

+0

這並不是合適的問題。如果人們不斷提出這樣的問題,那麼這裏就會有數萬億和數萬億的問題。你沒有使用'toLowerCase()'函數嗎? –

回答

-2

由於path是一個字符串,所以使用;

var path = window.location.pathname; // Returns path only 
var element = jq_menu("#topMenu a[href='" + path.toLowerCase() + "']"); 
element.parent().closest('li').addClass('active'); 
+0

我想將href值轉換爲LowerCase –

+1

請閱讀對其他答案的評論。此外,這在問題的評論中已經得到解決,所以除非您添加額外的東西,否則不需要它。 – Rajesh

+0

我想比較LowerCased'href'和LowerCased'path'並返回元素 –

0

您可以將href值轉換爲小寫只是像我下面做,它只是一個JavaScript對象值處理。你可以運行它:

// All is happening on load, just for example 
 
$(document).ready(function() { 
 
    // Before 
 
    console.log($('a#my-selector').attr('href')); 
 
    // Getting value and converting 
 
    var value = $('a#my-selector').attr('href').toLowerCase(); 
 

 
    // ..you can use it where you want now.. 
 

 
    // I'm just setting it back to the element.. 
 
    $('a#my-selector').attr('href', value); 
 
    // After 
 
    console.log($('a#my-selector').attr('href')); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<a id="my-selector" href="LINK-UPPER-CASE">I'm uppercase</a>

希望它能幫助!