Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 理解HTML锚定标记_Javascript_Html_Internet Explorer_Firefox_Anchor - Fatal编程技术网

Javascript 理解HTML锚定标记

Javascript 理解HTML锚定标记,javascript,html,internet-explorer,firefox,anchor,Javascript,Html,Internet Explorer,Firefox,Anchor,让我们从以下示例开始: 在同一目录中创建三个页面: test.html index.html 您的test.html: <html> <head> <script> function test() { alert('Going to google.com?'); window.location="http://google.com"; } </script> </head> <bod

让我们从以下示例开始:

在同一目录中创建三个页面: test.html index.html

您的test.html:

<html>
 <head>
  <script>
   function test()
   {
    alert('Going to google.com?');
    window.location="http://google.com";
   }
  </script>
 </head>
 <body>
  <a href='' onclick="test();">google.com</a><br/>
  <input type="button" value="google" onclick="test();" />
  <a href='' onmouseover="test();">google.com</a><br/> 
 </body>
</html>

功能测试()
{
警报(‘去谷歌网站?’);
window.location=”http://google.com";
}


现在检查IE上的test.html页面以及firefox或crome

你会注意到以下几点:

  • 按钮工作正常
  • 第一个超链接在IE和其他浏览器中的工作方式不同。在IE中,它将我们带回index.html页面,而在firefox中,它保持在同一页面上
  • 对于第一个超链接,window.location失败
  • 第二个超链接你们不能点击它,因为鼠标悬停事件将首先触发,而且它工作得非常完美 为什么?

    我最感兴趣的是第三点,因为它甚至会提醒我们window.location失败。

    试试这个:

    <html>
     <head>
      <script>
       function test()
       {
        alert('Going to google.com?');
        window.location="http://google.com";
        return false;
       }
      </script>
     </head>
     <body>
      <a href='' onclick="Javascript:return test();">google.com</a><br/>
      <input type="button" value="google" onclick="Javascript:return test();" />
      <a href='' onmouseover="Javascript:return test();">google.com</a><br/> 
     </body>
    </html>
    
    
    功能测试()
    {
    警报(‘去谷歌网站?’);
    window.location=”http://google.com";
    返回false;
    }
    

    试试这个:

    <html>
     <head>
      <script>
       function test()
       {
        alert('Going to google.com?');
        window.location="http://google.com";
        return false;
       }
      </script>
     </head>
     <body>
      <a href='' onclick="Javascript:return test();">google.com</a><br/>
      <input type="button" value="google" onclick="Javascript:return test();" />
      <a href='' onmouseover="Javascript:return test();">google.com</a><br/> 
     </body>
    </html>
    
    
    功能测试()
    {
    警报(‘去谷歌网站?’);
    window.location=”http://google.com";
    返回false;
    }
    


    JavaScript事件触发,
    窗口。位置设置为
    ,然后触发链接的默认操作,浏览器转到“”,它(IIRC)解析为当前URI

    如果你点击一个链接,然后快速点击另一个链接,你会得到同样的效果。在收到转到第二个浏览器的指令之前,浏览器没有时间转到第一个,它转到第二个。如果您延迟函数的返回(通过将警报放在第二个位置),它将为第一个URL的请求提供足够的时间,以便访问该URL而不是第二个URL

    您需要取消默认操作,当您使用内在事件属性(不推荐,这是前进的方向)时,该操作通过返回false来完成

    onclick="test(); return false;"
    

    JavaScript事件激发,
    window.location
    被设置,然后链接的默认操作激发,浏览器转到“”,它(IIRC)解析为当前URI

    如果你点击一个链接,然后快速点击另一个链接,你会得到同样的效果。在收到转到第二个浏览器的指令之前,浏览器没有时间转到第一个,它转到第二个。如果您延迟函数的返回(通过将警报放在第二个位置),它将为第一个URL的请求提供足够的时间,以便访问该URL而不是第二个URL

    您需要取消默认操作,当您使用内在事件属性(不推荐,这是前进的方向)时,该操作通过返回false来完成

    onclick="test(); return false;"
    

    为什么在返回语句之前有标签?如果我将
    alert()
    放在
    window.location
    之后,那么将首先调用
    alert()
    ,然后它将我们重定向到我们的位置为什么?事实上,我在js方面没有经验告诉你为什么在重定向之前会出现警报。在我看来,重定向似乎是在更改窗口对象的location属性时触发的,事件被放在堆栈上,并在单击事件之后进行处理,而单击事件现在正在test()方法中处理。为什么在返回语句之前有标签?如果我将
    alert()
    放在
    window.location
    之后,那么将首先调用
    alert()
    ,然后它将我们重定向到我们的位置为什么?事实上,我在js方面没有经验告诉你为什么在重定向之前会出现警报。在我看来,重定向似乎是在窗口对象的location属性发生更改时触发的,事件被放在堆栈上,并在您的test()方法中正在处理的click事件之后进行处理