如何为我的页面创建永久链接?

时间:2015-02-09 10:59:34

标签: javascript html css

我创建了一个包含60多个问题的常见问题解答页面,现在我想创建一个指向每个问题的链接,以便我可以在我的博客中提供链接,并且点击用户可以直接登陆该问题而不必浏览整个FAQ页面。那么我该如何创建链接呢?我只使用html,css和javascript。有人可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:1)

我建议您在常见问题解答页面顶部创建一个内容部分,例如可能会有问题。

这可以通过创建有序列表并列出您的问题来实现。

然后,您可以链接您的问题,以便当用户点击问题时,他会被定向到页面所在的确切位置。这可以使用锚来实现,就像Yavor说的那样。

您首先需要为每个问题创建一个ID,例如:

<div id="question1">Here is a question</div>
<div>Here is the answer to the question</div>

然后在有序菜单中,为常见问题解答中的相应问题创建一个锚:

<ul><li><a href="#question1">This will take you to question 1</a></li></ul>

答案 1 :(得分:0)

您可以使用锚点。

来自w3schools的例子:

为任何元素添加id属性:

<a id="tips">Useful Tips Section</a>

然后创建元素的链接(实用提示部分):

<a href="#tips">Visit the Useful Tips Section</a>

或者,从另一页面创建指向元素的链接(实用提示部分):

<a href="http://www.w3schools.com/html_links.htm#tips">Visit the Useful Tips Section</a> 

http://www.w3schools.com/html/html_links.asp

答案 2 :(得分:0)

添加以下脚本以平滑滚动

<a href="#question1">1. How to log out?</a><br/>

<h3 id="question1">1. How to log out?</h3>
<p>Answer...</p>

<script>
    $(function () {
        $('a[href*=#]:not([href=#])').click(function () {
            if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
                var target = $(this.hash);
                target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
                if (target.length) {
                    $('html,body').animate({
                        scrollTop: target.offset().top - 65
                    }, 1000);
                    return false;
                }
            }
        });
    });
</script>