简体   繁体   English

是否可以仅使用链接滚动到HTML文档的任何部分?

[英]Is it possible to scroll to any part of HTML document using only link to it?

So I want to use link like http://example.com/fo.html#line22 on ANY HTML page (meaning I can not change the page HTML code but I can read it before giving link) to scroll to line 22 of HTML document or knowing contents to some Known as existing line or button on that page. 因此,我想在任何HTML页面上使用诸如http://example.com/fo.html#line22之类的链接(这意味着我无法更改页面HTML代码,但在给出链接之前可以阅读它)滚动到HTML的第22行文档或知道该页面上某些已知行或按钮的内容。 How to do such thing? 怎么做这样的事情?

No, a named anchor or other element would need to be present on the page with the same id or name as supplied in the hash value of the url. 不,在页面上需要存在一个具有名称或名称的锚点或其他元素,其idname应与网址的哈希值中提供的idname相同。

http://en.wikipedia.org/wiki/Fragment_identifier#Examples http://en.wikipedia.org/wiki/Fragment_identifier#Examples

RFC 5147 proposes something similar for plain text documents only. RFC 5147仅针对纯文本文档提出了类似的建议。 However, none of the mainstream browsers have implemented this functionality yet. 但是,尚无主流浏览器实现此功能。

RFC 5147 proposes a fragment identifier for text/plain [read: not HTML] documents, based on character and line positions and ranges within the document using the keywords "char" and "line".[6] RFC 5147基于字符和行的位置以及文档中使用关键字“ char”和“ line”的范围,为文本/纯文本(而非HTML)文档提出了一个片段标识符。[6] This example identifies lines 11 through 20 of a text document, for instance: 本示例标识文本文档的第11至20行,例如:

There is no way you are going to be able to do this by just editing the link if you don't have a named anchor, as stated a couple times before in this thread, but... 如果您没有命名锚,则无法通过仅编辑链接来做到这一点,如之前在此线程中多次提到的那样,但是...

If you can read the document you can also substitute it. 如果您可以阅读文档,也可以用它代替。

  • read and cache the document, placing a tiny snip of html where you want to scroll to like this: 读取并缓存文档,在要滚动到的位置放置一小段html,如下所示:

    <span id="scroll-to-id"><!-- nothing in here --></span>

  • instead of the original url, give a link to your cached page with the id of your inserted snippet: 而不是原始的url,提供一个到您的缓存页面的链接,并带有您插入的代码段的ID:

    http://www.yourwebsite.com/scrollto.php ?{urlencoded-url-of-other-page}?scrollto={linenumber};#scroll-to-id

scrollto.php should fetch the html document, process and cache it according to linenumber. scrollto.php应该获取html文档,并根据行号对其进行处理和缓存。

You'll probably also want to insert a header into the cache stating you don't own the page and add a link to the page in its original context. 您可能还希望将标题插入到缓存中,以声明您不拥有该页面,并在其原始上下文中添加指向该页面的链接。

You'll still have to work out what constitutes a line to find the right place in the document, but you could probably load the external html into a DOMDocument , convert to text ( $document->documentElement->nodeValue should get you started) and work with that. 您仍然必须弄清楚什么构成一行才能在文档中找到正确的位置,但是您可以将外部html加载到DOMDocument中 ,转换为文本( $document->documentElement->nodeValue应该可以让您开始使用)并与之合作。

You can use the URl to link to any element with an ID or name attribute, but there's nothing else you can do to direct the browser unless you control the source. 您可以使用URl链接到具有ID或name属性的任何元素,但是除非您控制源, 否则您无法做任何其他操作来定向浏览器

IDs are usually found in many places on the page, so by reading through the source you might find something to grab onto. ID通常在页面上的许多地方都可以找到,因此通过阅读源代码,您可能会发现一些值得抓住的地方。 For example, you could link to http://example.com/fo.html#login_box to make the page scroll down to the login box below. 例如,您可以链接到http://example.com/fo.html#login_box,以使页面向下滚动到下面的登录框。

<div id="login_box">
   Username:
   Password:
</div>

This is going to be hard and painful. 这将是艰难而痛苦的。 I'd recommend to just scroll by pixels with help of window.scrollTo . 我建议在window.scrollTo帮助下按像素滚动。

window.onload = function() {
    var line = parseInt(location.hash.replace(/#line/, ''));
    if (line) window.scrollTo(0, 10 * line);
};

This way page#line22 will scroll down by 220 pixels. 这样page#line22将向下滚动220个像素。 You can modify the pixels-per-line in the code (which is currently 10 ). 您可以修改代码中的每行像素数(当前为10 )。

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

相关问题 如何在新链接中传递部分url? 仅使用HTML和PHP - How to pass part of url in new link? Using only HTML & PHP 仅下载HTML文档的一小部分 - Downloading only a small part of an HTML document 滚动指示器 JavaScript 仅适用于 HTML 文档 - Scroll indicator JavaScript only works in HTML document 粘性顶部菜单具有透明度(仅滚动文档的一部分) - Sticky top menu with transparency (scroll only part of the document) 使用 HTML 链接 rel 导入 HTML 文档 - Import HTML document using HTML link rel IE 10中,document.links不能使用链接的ID进行访问,而只能使用索引进行访问 - IE 10, document.links does not possible to access using the id of the link but only with index 是否可以使用Javascript或任何服务器端技术从移动设备的源代码中删除HTML的某些部分? - Is it possible to remove some part of HTML from source for mobile using Javascript or any server side technique? 仅滚动页面的一部分 - Scroll only a part of a page 如何使引导程序仅影响html文档的一部分 - How to make bootstrap affect only a part of an html document 是否可以仅使用 JavaScript 而不使用 HTML 将动态链接插入到电子邮件中? (使用 Google Script 发送电子邮件) - Is it possible to insert a dynamic link into an email using only JavaScript and no HTML? (Using Google Script to sent the email)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM