jquery:如果 url 包含 #work 然后做一些事情

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5757362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:46:41  来源:igfitidea点击:

jquery: if url contains #work then do something

jqueryurlcontain

提问by Eddie

I tried to write a script which allow me to load certain events when I enter specific url.

我试图编写一个脚本,当我输入特定的 url 时,它允许我加载某些事件。

My code looks like this:

我的代码如下所示:

$(function(){
    var url = window.location.pathname;
    $("url:contains('#Work')").animate({"left": "250"}, "slow");
});

But it doesnt work. Any suggestions? Any help is appreciated.

但它不起作用。有什么建议?任何帮助表示赞赏。

回答by Ketan Modi

$(function() {
    if ( document.location.href.indexOf('#Work') > -1 ) {
        $('#elementID').animate({"left": "250"}, "slow");
    }
});

回答by Paul

window.location.href is pulling the URL into a variable, so you can't search for #Work using that method. Try:

window.location.href 将 URL 拉入变量中,因此您无法使用该方法搜索 #Work。尝试:

var url = window.location.href;

if (url.search("#Work") >= 0) {
    //found it, now do something
}