【问题标题】:Is it possible to point to a section of the page without anchors?是否可以指向没有锚点的页面部分?
【发布时间】:2015-10-05 18:04:58
【问题描述】:

我正在尝试链接到另一个页面(我无法修改)。不幸的是,该页面没有每个部分的唯一名称或 ID。相反,节标题只是在 h2 标记内。

例如:

<h2>Section A</h2>
<h2>Section B</h2>
<h2>Section C</h2>
<h2>Section D</h2>
<h2>Section E</h2>
<h2>Section F</h2>
<h2>Section G</h2>
<h2>Section H</h2>
<h2>Section I</h2>
<h2>Section J</h2>

基本上,我想将我的链接指向“F 部分”。但是,没有锚,这甚至可能吗?

【问题讨论】:

  • 这在 JS 中绝对是可能的。通过某种方式找到你想要的h2标签,也许是getElementsByTagName中的字母索引,然后确定它在页面上的位置并使用window.scrollTo
  • @Ryan OP 甚至没有提到id's。
  • 如果您能够使用javascript来操作页面,例如访问加载到iFrame中的目标页面,那么是的,您可以。否则,您对外部页面无能为力。

标签: html hyperlink


【解决方案1】:

使用来自Get the position of a div/span taggetPos

goTo( "Section C" ); 为例。

function goTo( text )
{
    var h2s = document.getElementsByTagName( 'h2' );

    for( var i = 0; i < h2s.length; ++i )
    {
        if( h2s[i].innerHTML.trim() == text.trim() )
        {
            window.scrollTo( h2s[i] );
            return;
        }
    }
}

*未经测试,但应该能让你接近。

【讨论】:

    猜你喜欢
    • 2011-01-19
    • 2017-10-23
    • 2023-03-05
    • 1970-01-01
    • 2010-11-27
    • 2020-08-16
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    相关资源
    最近更新 更多