douyi3676
douyi3676
2011-10-09 06:07
浏览 37
已采纳

想要在window.onload = init上使用javascript; 锚定“#top”

I am facing a problem with the java-script code...

html:

<div id="thumbs">
  <a href="link1.htm"><img src="images/Button_Watch.png"></a>
</div>

Javascript:

// This function runs when the page loads and adds click events to the links

function init() {
  var links = document.getElementById('thumbs').getElementsByTagName('a');
  // ...
}

// Call our init function when the page loads

window.onload = init;

The code will take the link "link1.htm" on clicking a button image link under div id "thumb" to show dynamic content from outside the server without reloading the page. The content show in (<div id="embed"></div>). I want the same "Button_Watch.png" when click to scroll to the anchor #top to point to the part where the content changes dynamically(ie; in <div id="embed"> position )

I think you got what I meant to convey, please help me

图片转代码服务由CSDN问答提供 功能建议

我遇到了java脚本代码的问题......

html:

 &lt; div id =“thumbs”&gt; 
&lt; a href =“link1.htm”&gt;&lt; img src =“images / Button_Watch  .PNG“&GT;&LT; / A&GT; 
&LT; / DIV&GT; 
   
 
 

使用Javascript:

 <代码> /  /当页面加载并将点击事件添加到链接时运行此函数
 
函数init(){
 var links = document.getElementById('thumbs')。getElementsByTagName('a'); 
 // ..  。
} 
 
 //在页面加载时调用我们的init函数
 
window.onload = init; 
   
 
 

代码将采用链接 “link1.htm”单击div id“thumb”下的按钮图像链接,以显示来自服务器外部的动态内容,而无需重新加载页面。 内容显示在(&lt; div id =“embed”&gt;&lt; / div&gt; )中。 当点击滚动到锚 #top 指向内容动态变化的部分(即&lt; div id =“embed”时,我想要相同的“Button_Watch.png” &gt; 位置)

我认为你有意思转达,请帮帮我

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumaque6551
    doumaque6551 2011-10-09 06:34
    已采纳

    Here's a quick and simple solution.

    Javascript:

    function scrollToAnchor (anchor_name) {
      document.getElementsByName(anchor_name)[0].scrollIntoView(true);
    }
    

    HTML:

    <a name="top"></a>
    
    <!-- other content -->
    
    <a href="link1.htm" onclick="scrollToAnchor('top');">
      <img src="images/Button_Watch.png">
    </a>
    
    点赞 评论

相关推荐