如何在选择器条件中使用toLowerCase在jQuery中

时间:2017-01-09 11:26:47

标签: javascript jquery selector

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

我将此代码用于选择活动菜单。

我想在第2行将href值转换为lowerCase

我该怎么做?

2 个答案:

答案 0 :(得分: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>

希望它有所帮助!

答案 1 :(得分:-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');