---
title: "元素内联事件的真相"
author: "zhouyrt"
date: "2010-12-13T18:04:08Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2010-12-13T20:02:53Z"
id: 841055
url: "https://snandy.iteye.com/blog/841055"
---

## 引子

是JS1群中的Mead Lai提出的问题，如下

```
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Test case</title>
</head>
<body>

<input type="button" onclick="open();" value="Click me" />
<a href="javascript:open();">Click me</a>

<script type="text/javascript">   
    function open(){
        alert(5);
    }
</script>

</body>
</html>
```

   
为何点击按钮和链接后的效果不一样？点击按钮后页面重载入了，点击链接弹出了5。

部分人（一些有经验的）测试后没找到根本原因，惊喜的发现了open是一个全局方法（window上的）。  
把原因归结为不该命名为与window.open同名。认为这样会产生诡异的现象，然后就上升到命名问题，大肆喧哗这样的同名讨论没意思，没必要---因为根本不会这么写。

是的，不应该这么写。但真相呢？

 

## 分析

完整分析在这：[http://www.w3help.org/zh-cn/causes/SJ9009](http://www.w3help.org/zh-cn/causes/SJ9009)

慢慢品 w3help 精彩，详细的分析。答案尽在其中..