Php:如何将URL与ID为html部分的ID相关联?

时间:2013-10-03 11:32:56

标签: php urlvariables

php新手:)

在博客项目上工作时,我想通过点击索引页面上的“评论”链接,将用户导航(发送)到具有特定(完整)帖子的其他页面及其评论。

我希望将用户直接发送到评论div。但是我还必须在问号“?”之后提供带有链接的帖子ID。我想知道在URL中包含#comments div(html部分)的位置。

提前谢谢你。

3 个答案:

答案 0 :(得分:3)

您可以使用空<a>标记执行此操作:

在您的评论<div>之前粘贴此内容:

<a name="comments"></a>

尝试使用#comments标记进入您的网页,例如:http://mysite.com/example.php?postID=123#comments

答案 1 :(得分:3)

首先,将id分配给您希望浏览器(用户)滚动到的元素。

实施例

<div id="mycomment">

然后,生成与您选择的#id追加的网址。

实施例

http://example.com/post/1/#mycomment

答案 2 :(得分:1)

为锚标记<a name='anchor1'>添加名称,并使用前缀#<a href='#anchor1'>Comment</a>将其链接到href

<a href="#anchor1">First</a> |
<a href="#anchor2">Second</a> |
<a href="#anchor3">Third</a> |
<a href="#anchor4">Forth</a> |
<a href="#anchor5">Fifth</a> 

<a name='anchor1'>First comment header</a>
<p>First comment content<br>First comment content<br>First comment content<br>First comment content<br>First comment content<br>First comment content<br>First comment content<br>First comment content<br>First comment content<br>First comment content<br>First comment content<br>First comment content<br>First comment content<br>First comment content<br></p>

<a name='anchor2'>Second comment header</a>
<p>Second comment content<br>Second comment content<br>Second comment content<br>Second comment content<br>Second comment content<br>Second comment content<br>Second comment content<br>Second comment content<br>Second comment content<br>Second comment content<br>Second comment content<br>Second comment content<br>Second comment content<br>Second comment content<br></p>

<a name='anchor3'>Third comment header</a>
<p>Third comment content<br>Third comment content<br>Third comment content<br>Third comment content<br>Third comment content<br>Third comment content<br>Third comment content<br>Third comment content<br>Third comment content<br>Third comment content<br>Third comment content<br>Third comment content<br>Third comment content<br>Third comment content<br></p>

<a name='anchor4'>Fourth comment header</a>
<p>Fourth comment content<br>Fourth comment content<br>Fourth comment content<br>Fourth comment content<br>Fourth comment content<br>Fourth comment content<br>Fourth comment content<br>Fourth comment content<br>Fourth comment content<br>Fourth comment content<br>Fourth comment content<br>Fourth comment content<br>Fourth comment content<br>Fourth comment content<br></p>

<a name='anchor5'>Fifth comment header</a>
<p>Fifth comment content<br>Fifth comment content<br>Fifth comment content<br>Fifth comment content<br>Fifth comment content<br>Fifth comment content<br>Fifth comment content<br>Fifth comment content<br>Fifth comment content<br>Fifth comment content<br>Fifth comment content<br>Fifth comment content<br>Fifth comment content<br>Fifth comment content<br></p>

如果评论详情在其他页面中,请将其称为

<a href="other_page.php#anchor1">First</a> |
<a href="other_page.php#anchor2">Second</a> |
<a href="other_page.php#anchor3">Third</a> |
<a href="other_page.php#anchor4">Forth</a> |
<a href="other_page.php#anchor5">Fifth</a>