【问题标题】:gwt anchor tag not anchoringgwt 锚标记未锚定
【发布时间】:2010-12-07 20:56:09
【问题描述】:

为什么浏览器不滚动到锚点?
网址:http://localhost:8080/index.html#myAnchor3

this.anchor1.setName("myAnchor1");
this.add(this.anchor1);
this.anchor2.setName("myAnchor2");
this.add(this.anchor2);
this.anchor3.setName("myAnchor3");
this.add(this.anchor3);

是不是因为锚点是在页面加载完成后创建的,所以浏览器在尝试滚动到它时看不到锚点?

【问题讨论】:

    标签: gwt anchor


    【解决方案1】:

    试试这个:

    this.anchor.setName("myAnchor");
    this.add(this.anchor);
    location.hash = '#myAnchor';
    

    是的,你是对的,你的锚点是在页面加载后创建/插入的,所以......

    【讨论】:

    • location.hash 在什么范围内? location 在范围内,但它只有访问器方法,没有直接访问或设置器方法。
    • @antony:你是说它不适合你。它对我有用。完整地说,它是“window.location.hash”(参见developer.mozilla.org/en/DOM/window.locationmsdn.microsoft.com/en-us/library/ms533775%28VS.85%29.aspx)。它可以被读取/分配。
    • 啊,你跳到了javascript。
    • @anthony: ermm,你编码的一切都是javascript,不是吗?如果您希望散列根据 URL 中提供的散列是动态的,则设置“location.hash = location.hash;”创建锚点之后。
    • @anthony:抱歉,我怎么能错过。如果您的锚点是在呈现给浏览器之前定义的,它应该可以工作。您检查了 HTML 标记吗?你应该看到一个 '
    【解决方案2】:

    您可以尝试使用 Element.scrollIntoView(),它不仅会滚动窗口,还会滚动 DOM 层次结构中包含该元素的任何可滚动容器。

    【讨论】:

    • 看起来很有希望,我今晚试试。
    • 还没有让它工作,但不相信我没有做错什么。
    • 我试图滚动到一个尚未添加到 DOM 的元素。重构一些代码...
    【解决方案3】:

    必须重写 onLoad 方法,并在那里调用 scrollIntoView,否则它会尝试滚动到尚未添加到 DOM 的对象。

    public class Foo extends Widget
    {
      Foo(){
      }
    
      @Override
      protected void onLoad(){
        super.onLoad();
        getElement().scrollIntoView();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-03
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      • 1970-01-01
      • 2021-12-06
      • 2014-06-09
      • 2018-09-07
      • 1970-01-01
      相关资源
      最近更新 更多