当单击特定链接时,我正在使用此代码段将内容淡化到 div 中....
<script language="JavaScript">
$(document).ready(function () {
$('#section1, #section2, #section3').addClass('js');
$('#content-container a').click(function (e) {
e.preventDefault();
var rel = $(this).attr('rel');
$('#' + rel).fadeIn().siblings('div').fadeOut();
});
var link = document.URL.split('#')[1];
if (link) {
var el = $('#' + link);
if (el) el.click();
}
});
</script>
有没有一种方法可以使用 URL 而不是单击来加载指定的内容?所以我可以链接到 www.mydomain.com/mypage.php#section2 或类似的东西?
更新
这是简化代码的 jsfiddle http://jsfiddle.net/k6RhR/
最佳答案
你可以这样做:
if(window.location.hash && window.location.hash=="#section2") {
// Do stuff that are meant to happen when this hash is present.
}
关于javascript - 使用 URL 参数触发 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22224118/