阅读 172

jq 地址截取方法

1.地址栏带参数跳转

//项目编号
var projectKindName =expands[i].project_kind.kind.name;
var projectKindId =expands[i].project_kind.kind.id;
var projectKindTarget =expands[i].project_kind.form_for_kind;

//转换跳转地址
var fullUrl ='projectKindName+'&kind_id='+projectKindId+'&target='+projectKindTarget;
     <a href="/home/41?'+fullUrl+'">跳转</a>
//跳转后截取所要的参数
function getPar(par){    //获取当前URL
    var local_url = document.location.href;
    //获取要取得的get参数位置
    var get = local_url.indexOf(par +"=");
    if(get == -1){
        return false;
    }
    //截取字符串
    var get_par = local_url.slice(par.length + get + 1);
    //判断截取后的字符串是否还有其他get参数
    var nextPar = get_par.indexOf("&");
    if(nextPar != -1){
        get_par = get_par.slice(0, nextPar);
    }
    return get_par;
}


getPar("projectKindName")//得到value

复制代码

2.获取当前页面参数(key)方法

function getPar(par){
    //获取当前URL
    var local_url = document.location.href;
    //获取要取得的get参数位置
    var get = local_url.indexOf(par +"=");
    if(get == -1){
        return false;
    }
    //截取字符串
    var get_par = local_url.slice(par.length + get + 1);
    //判断截取后的字符串是否还有其他get参数
    var nextPar = get_par.indexOf("&");
    if(nextPar != -1){
        get_par = get_par.slice(0, nextPar);
    }
    return get_par;
}复制代码

3.截取当前页面路径

var full_url = location.href;
var split_rul = full_url.split('?');
//取到?号 前面的 路径
split_rul[0]复制代码

4.导航高亮

//1.导航点击高亮背景(万能)
//css样式
.current-nav-active{
    background: #36364c;
}//html代码
<li class="" id="week_nav">
    <a href="##" onclick="weekly()">周计划</a>
</li>
<li class="" id="day_nav">
    <a class="dropdown-toggle" data-toggle="dropdown">日班计划</a>
    <ul class="dropdown-menu">
        <li><a href="#" onclick="daily()">日班图表</a></li>
        <li><a href="#" onclick="daily()">日班列表</a></li>
        <li><a href="#" onclick="daily()">冲突检测</a></li>      
    </ul>
</li>
//js代码
var current_option ={
 "day":1,
"week":0
};function weekly(){
    current_option.week = 1;
    current_option.day = 0;
    localStorage.current_option = JSON.stringify(current_option);
}
function daily(){
    current_option.week = 0;
    current_option.day = 1;
    localStorage.current_option = JSON.stringify(current_option);
}
if(localStorage.current_option){
    current_option = JSON.parse(localStorage.current_option);
}
if(current_option.day){
    $("#day_nav").addClass("current-nav-active");
    $("#week_nav").removeClass("current-nav-active");

}else if(current_option.week) {
    $("#day_nav").removeClass("current-nav-active");
    $("#week_nav").addClass("current-nav-active");
}


2.截取当前地址方式 导航高亮
//导航点击背景
var full_url = location.href;
var split_rul = full_url.split('?');
$(function(){
    $("#menuUL a").each(function(){
        if($(this).attr('href') == split_rul[0]){
            $(this).css({'background-color':'#211013','border-color':'#f49d3c','box-shadow':'inset 0 0 10px #B2620D;'});
        }
    })

});
复制代码