jquery: if url contains #work then do something

こ雲淡風輕ζ 提交于 2019-12-17 22:46:57

问题


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.


回答1:


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



回答2:


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
} 


来源:https://stackoverflow.com/questions/5757362/jquery-if-url-contains-work-then-do-something

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!