---
title: "IE6用setAttribute添加事件无效"
author: "413899327"
date: "2013-09-08T11:51:16Z"
tags: []
categories: ["Web前端"]
slug: "ie6-setattribute"
draft: false
lastmod: "2013-09-08T11:51:16Z"
id: 1938725
url: "https://rogerfederer.iteye.com/blog/1938725"
---

```html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IE6用setAttribute添加事件无效</title>
<style type="text/css">
* {margin:0;padding:0;margin:0 auto;}
#div1{width:200px; height:200px;background:#F03;}
</style>
</head>

<body>
<div id="div1"></div>
<script type="text/javascript">

function method(){
	alert(1);
}
var ids = "div1";

document.getElementById(ids).setAttribute("onclick","method()"); 
//document.getElementById(ids).setAttribute("onclick",method); //无效



</script>
</body>
</html>


```



在IE6 下无法响应该事件 

IE7+、chorome、Firefox 条件下正常,会响应onclick事件！