如果URL包含字符串然后运行jQuery函数?

Lia*_*iam 4 javascript jquery

在我的网站上,如果我的网址包含"测试"一词,我就会运行jQuery函数

我试图做的就是如果我的网址包含'rest'字符串,那么为页面上的元素添加边距?

我添加了一个jSfiddle试图展示到目前为止我做了什么.

$(document).ready(function(){         
    // How can I check to see if my url contains the word 'TEST' then run the below function?
    $('.block-widget').css('margin-top, 252px')     
});
Run Code Online (Sandbox Code Playgroud)

Sus*_* -- 9

使用window.location来获得当前位置的URL ..

根据条件,您可以应用margin top属性.

$(document).ready(function(){         
     var pathname = window.location.pathname;
     if(pathname.indexOf('text') > -1){
        $('.block-widget').css('margin-top, 252px');
     }     
});
Run Code Online (Sandbox Code Playgroud)