我尝试编写一个脚本,允许我在输入特定 url 时加载某些事件。
我的代码如下所示:
$(function(){
var url = window.location.pathname;
$("url:contains('#Work')").animate({"left": "250"}, "slow");
});
但它不起作用。有什么建议么?任何帮助表示赞赏。
$(function() {
if ( document.location.href.indexOf('#Work') > -1 ) {
$('#elementID').animate({"left": "250"}, "slow");
}
});
window.location.href 将 URL 拉入变量中,因此您无法使用该方法搜索 #Work。尝试:
var url = window.location.href;
if (url.search("#Work") >= 0) {
//found it, now do something
}