简体   繁体   English

对链接到元素ID的反应

[英]React on link to element id

I've made a website for my friends. 我已经为我的朋友们建立了一个网站。 It's one page where you navigate by scrolling: 这是您通过滚动浏览的一页:

www.hbzmusic.de www.hbzmusic.de

The both wish, that the different section could be targeted by direkt links such as hbzmusic.de/Musik . 双方都希望其他部分可以通过诸如hbzmusic.de/Musik之类的直接链接作为目标 So I decided to make a new directory /Musik (in the directory of index.php) with a new index.php simply linking back to the original one: 因此,我决定创建一个新目录/ Musik(在index.php目录中),并使用一个新的index.php,只需将其链接回原始目录即可:

<?php
    header('Location: ../index.php');
    die();
?>

But so far I have no argument to pass for scrolling to the corresponding section. 但是到目前为止,我还没有传递滚动到相应部分的参数。 Some students at my university suggested using # to target the id of the desired element: 我大学的一些学生建议使用#定位所需元素的ID:

header('Location: ../index.php#Musik');

But as my scroll function is made in javascript I am searching for a possibility to catch this #Musik to scroll to this element. 但是,由于我的滚动功能是用JavaScript制作的,因此我正在寻找一种捕获此#Musik滚动至此元素的可能性。

I hope this question isn't as dumb as it seems and I am not wasting someones time :). 我希望这个问题不会像看起来那样愚蠢,并且我不会在浪费别人的时间:)。

Thanks for any help! 谢谢你的帮助!

With javascript you can catch the fragment identifier 使用javascript可以捕获片段标识符

if(window.location.hash) {
    var hash = window.location.hash.substring(1);
    // scrollingToHash(hash);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM