OK,我正在制作我的网站的新版本:http://themes.visualise.ca/visualise/
当你通过点击图片选择一个项目时,它会在现有页面中动态加载所请求的wordpress项目页面,并更改url以包含哈希,所以我得到:http://themes.visualise.ca/visualise/#project
如果我把这个直接url给别人,Wordpress会自动加载项目页面,就好像用户点击了页面内的图像。
一切都是我想要的,除了一件事,我的代码使项目页面("#board")加载所有的时间,即使没有#项目哈希在url,所以我想使代码条件
$(window).load(function(e){
$.ajaxSetup({cache:false});
var post_slug = window.location.hash.substring(1);
$("#board").load("http://themes.visualise.ca/visualise/ajax/",{slug:post_slug});
$("#board").delay(1500).slideDown("slow");
});
http://url, http://url/, http://url/#, http://url/#/不应该加载项目板,只有http://url/#project或http://url/#whatever应该
但是我不知道怎么做,因为我对jQuery和编程还是新手。
应该是这样的:
$(window).load(function(e){
$.ajaxSetup({cache:false});
var post_slug = window.location.hash.substring(1);
if(/^[a-zA-z0-9-_]+$/.test(post_slug)){ //This conditions loading
$("#board").load("http://themes.visualise.ca/visualise/ajax/",{slug:post_slug});
$("#board").delay(1500).slideDown("slow");
}
});
我们问的是是否存在字符串哈希
EDIT:我已经更新了条件,使用正则表达式来满足您最近注释的需求
希望能有所帮助