如果URL包含字符串然后运行jQuery函数? [英] If URL contains string then run jQuery function?
本文介绍了如果URL包含字符串然后运行jQuery函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的网站上我喜欢运行jQuery函数,如果我的网址包含'test'这个单词
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?
我添加了一个jSfiddle来尝试显示到目前为止我做过的事情。
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
获取当前位置的网址..
Use window.location
to get the current location url..
根据条件,您可以应用margin top属性。
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');
}
});
这篇关于如果URL包含字符串然后运行jQuery函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文