javascript - 如何获取 url 参数并添加事件类 witt javascript/jquery?

标签 javascript jquery

我的链接是这样的:

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/

相关文章:

javascript - 有没有一个express javascript es6字符串模板引擎

javascript - 拆分日期值

javascript - 如何根据行索引获取行的列数据

jQuery div点击通过div并调用两个函数

jquery - 图像在高度转换后失去抗锯齿

javascript - 处理多个 protected 路由

javascript - Angularjs在js中绑定(bind)动态表单

javascript - 用数字在表格中用逗号替换点

javascript - 替换 json 对象中的单词

javascript - 限制 JQuery/ajax 中的列表选择