如果URL包含字符串,然后运行jQuery函数?(If URL contains string then run jQuery function?)

在我的网站id喜欢运行一个jQuery函数,如果我的网址包含单词'test'

所有我试图做的是如果我的网址包含'休息'字符串,然后添加页面上的元素的边距?

我添加了一个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') });

On my website id like to run a jQuery function if my url contains the word 'test'

All im trying to do is if my url contains the 'rest' string then add a margin to an element on the page?

Ive added a jSfiddle to try and show what Ive done so far.

$(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') });

最满意答案

使用window.location获取当前位置的网址..

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

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

Use window.location to get the current location url..

Based on the condition you can then apply the margin top property.

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

更多推荐