如何链接HTML部分

How to link HTML sections?

本文关键字:HTML 部分 链接 何链接      更新时间:2023-09-26

我对网页设计和HTML、JS、CSS有点陌生。

-我创建了一个包含基本标题、导航栏、图像滑块和基本部分的页面。主页看起来像这个自动取款机;http://i65.tinypic.com/98a4gh.jpg

我想打开"程序"部分,将其作为一个具有相同标题的空页面。因此,我复制了代码,创建了一个新的html文件,并将其链接到"程序"

<li><a href="example.html#section1" class="sec" name="section1">Programs</a></li> 

正如您所知,它在相同的选项卡中打开一个新的html文件,该文件正文为空("example.html#section1"包含一个空正文)(http://i68.tinypic.com/dorzfk.png)

问题是有没有办法创建该部分的直接链接(使用任何语言)。我的意思是它不应该使用新的html文件或iframe。我花了很多时间搜索,但我觉得我找不到合适的东西。T_T

当然有一种方法可以用javascript从头开始。像这样的东西;

如何使用链接调用JavaScript?这将是一个很好的开始。

我确实想插入我的两分钱,这将是研究一个像bootstrap和/或foundation这样的框架,其中许多东西已经设置好,供您开箱即用。学会自己做这件事很重要,但这些框架将帮助你在开始陷入困境之前了解全局。

编辑冗余:

来自用户EndangeredMassa:

<html>
<head>
     <script type="text/javascript">
        // Wait for the page to load first
        window.onload = function() {
          //Get a reference to the link on the page
          // with an id of "mylink"
          var a = document.getElementById("mylink");
          //Set code to run when the link is clicked
          // by assigning a function to "onclick"
          a.onclick = function() {
            // Your code here...
            //If you don't want the link to actually 
            // redirect the browser to another page,
            // "google.com" in our example here, then
            // return false at the end of this block.
            // Note that this also prevents event bubbling,
            // which is probably what we want here, but won't 
            // always be the case.
            return false;
          }
        }
    </script>
</head>
<body>
    <a id="mylink" href="http://www.google.com">linky</a>        
</body>
</html>