jquery: if url contains #work then do something

2019-01-22 13:23发布

I tried to write a script which allow me to load certain events when I enter specific 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.

2条回答
冷血范
2楼-- · 2019-01-22 13:41

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

var url = window.location.href;

if (url.search("#Work") >= 0) {
    //found it, now do something
} 
查看更多
贪生不怕死
3楼-- · 2019-01-22 13:50
$(function() {
    if ( document.location.href.indexOf('#Work') > -1 ) {
        $('#elementID').animate({"left": "250"}, "slow");
    }
});
查看更多
登录 后发表回答