我的链接是这样的:
index.php
index.php?do=that
index.php?do=this
我当前的 javascript 代码是这样的:
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/\/$/,'') + "$");
console.log(url);
$('.nav-link').each(function(){
if(urlRegExp.test(this.href.replace(/\/$/,''))){
$(this).addClass('active');
}
});
});
因此,即使我在 index.php?do=this
,也可以使用此代码控制台日志对我说 /path/index.php
我想我需要对 var url = window.location.pathname
进行更改线,但我不能。
我应该如何改进我的代码? SO 中有很多类似的问题,但不是同一个问题。
感谢任何帮助。
最佳答案
在 PHP 中,您可以从 GET 数组获取 URL 参数。 $_GET['do'] 或尝试打印 $_GET。该数组将打印所有 URL 参数。 希望这有帮助!
关于javascript - 如何获取 url 参数并添加事件类 witt javascript/jquery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36398558/