如何重定向到 html 页面的特定部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30458037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:31:50  来源:igfitidea点击:

How to redirect to a particular section of a page in html

html

提问by Mayank Parnami

Please help me with the code to redirection to a particular section of a page .

请帮助我使用重定向到页面特定部分的代码。

I am done with redirection to target page but what I want is to redirect to particular <div>of the page .

我已完成重定向到目标页面,但我想要的是重定向到特定<div>页面。

Thanks in advance

提前致谢

回答by Ajai

You can do it in two ways.

您可以通过两种方式做到这一点。

1) [via Javascript (+jQuery)]

1) [通过 Javascript (+jQuery)]

<a href="#" id="home">home</a>

$('#home').click(function(){
$(document).scrollTop(100) // any value you need
});

2) [via pure HTML]

2) [通过纯 HTML]

<a href="#home_section">home</a>

<section id="home_section"></section>

回答by Tharif

<div id="go1">
<!--  content -->
</div>

<div id="go2">
<!-- content -->
</div>

<div id="go3">
<!-- content -->
</div>

...

Just append url id as below ,you are done !

只需添加如下的 url id,你就完成了!

news.html#go1
news.html#go2
news.html#go3

回答by Tharif

Basically you use anchor tags in HTML to get your job done. You'l probably be familiar with them as:

基本上,您可以使用 HTML 中的锚标记来完成工作。您可能对它们很熟悉,因为:

<a href = " "> </a>

As HTML convention, while defining a section, you can give each section an ID for identifiers :

作为 HTML 约定,在定义一个部分时,您可以为每个部分提供一个 ID 作为标识符:

<section id= "blahblah" ></section>

And you can redirect to the section by just mentioning them in the anchor tags :

您可以通过在锚标签中提及它们来重定向到该部分:

<a href = "#blahblah"> </a>

回答by JPG

This is the easiest way for me

这对我来说是最简单的方法

<li><a href="#prices">Prices</a></li> (This could be a paragraph or a button)</li>

<div id="prices">
   // Prices section

</div>

回答by antyrat

You need to add idattribute to that section of page you want to show and pass idname at the end of url using hash (#) symbol. For example you want to redirect user to div with id='test'

您需要将id属性添加到要显示的页面部分,并id使用哈希 ( #) 符号在 url 的末尾传递名称。例如,您想将用户重定向到 divid='test'

<div id="test">your section content</div>

Then you should use this url structure:

那么你应该使用这个 url 结构:

http://example.com/your_page.php?some_param=1#test

回答by Hitesh Nagpal

Here are two conditions,

这里有两个条件,

1) If you want to redirect to div from different page:

1) 如果你想从不同的页面重定向到 div:

<a href="page.html#div-panel">Page</a>

if(window.location.href.includes("div-panel"))
{
    $(document).scrollTop(450);
}

2) If you want to redirect to div in same page:

2) 如果要重定向到同一页面中的 div:

<button id="button-click">Panel</button>

$('#button-click').click(function(){
    $(document).scrollTop(450);
});

回答by Felipe A.

You can link the html code with css.

您可以使用 css 链接 html 代码。

In c#

在 C# 中

Response.Redirect("http://www.example.com/index.aspx#id_in_css");

回答by K K

Give that section an id (lets say: section1) and then the redirect url will be http://www.sample.com/page#section1.

为该部分提供一个 id(可以说section1:),然后重定向 url 将是http://www.sample.com/page#section1.

Note: the # and the keyword, that's the id of the section you want your browser to scroll to.

注意:# 和关键字是您希望浏览器滚动到的部分的 ID。

Read more about Fragment Identifier here

在此处阅读有关片段标识符的更多信息