它可能是这样的:
$(window).load(function() {
// get hash string eg. #video1
var hash = window.location.hash;
if (hash == '#video-1') { // do some checks, you can apply regex or switch
// I'm not sure what your html looks like
$(".main_stage iframe").prop("src", $('selector').attr("href"));
loadURL($(this).attr('href'));
}
});
$(".thumbnail-1,.thumbnail-2,.thumbnail-3").on("click", function(event) {
var className = $(this).attr("class"); // get class name of clicked element
var videoId = className.split("-").pop(); // split string to get video id => "1, 2, 3 etc."
window.location.hash = '#video' + videoId; // change url to eg. #video1
event.preventDefault();
$(".main_stage iframe").prop("src", $(event.currentTarget).attr("href"));
loadURL($(this).attr('href'));
});
如果您要基于正则表达式进行验证(此处为小提琴),您可以使用以下代码:
var hash = window.location.hash;
var regex = new RegExp('^#video-[0-9]{1,10}$');
if (regex.test(hash)) {
// if hash is something like "#video-[NUMBERS]" then do something
}