无法向ID添加事件。虽然能够附加事件侦听器onLoad,但同样无效用于onClick或点击。有人可以帮我解决吗?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" id="iphone-viewport" content="minimum-scale=1.0, maximum-scale=1.0, width=device-width">
<title>Astro Wheel</title>
<script type="text/javascript">
function loaded() {
alert("Loaded")
var ele= document.getElementById("aries");
if (window.attachEvent)
{ ele.attachEvent('click', testFunction); }
else if (window.addEventListener)
{ ele.addEventListener('click', testFunction, true); }
// I am unable to register a event on #aries, below alerts giving "null" and "undefined" respectively
alert(ele.onclick);
alert(ele.click)
}
function testFunction(){
alert("Clicked")
}
window.addEventListener("load", function() { setTimeout(loaded, 100); }, true);
</script>
<style type="text/css">
body, ul, li, div, p { padding:0; margin:0; }
body { background:#444 url(bg.png); }
#wheel { position:absolute; z-index:1; width:320px; height:321px; overflow:hidden; }
#display1 { position:absolute; z-index:100; width:320px; height:321px; }
ul { position:absolute; z-index:10; width:320px; height:320px;
-webkit-transform-origin:50% 50%;
-webkit-transform:rotateZ(0rad);
text-align:center;
}
ul li { position:absolute; z-index:11; width:76px; height:88px; left:122px; top:22px; overflow:hidden;
-webkit-transform-origin:38px 138px;
-webkit-transition-timing-function:ease-out;
-webkit-transition-duration:800ms;
}
#aries {-webkit-transform:rotate(0deg) translate(0, -0px); }
#cancer {-webkit-transform:rotate(90deg) translate(0, -000px); }
#libra {-webkit-transform:rotate(180deg) translate(0, -0000px); }
#caprico { -webkit-transform:rotate(270deg) translate(0, -000px); }
</style>
</head>
<body>
<div id="wheel">
<div id="display1"></div>
<ul id="zodiac">
<li id="aries"><div>Aries</div></li>
<li id="cancer"><div>Cancer</div></li>
<li id="libra"><div>Libra</div></li>
<li id="caprico"><div>Caprico</div></li>
</ul>
<div id="ok"></div>
</div>
</body>
</html>
P.S。我不允许在基于WEBKIT的移动浏览器和谷歌浏览器上使用任何框架。测试
答案 0 :(得分:3)
使用addEventListener
(或IE的attachEvent
)不会更改onclick
属性的内容 - 它们是独立的。
不应该有时间延迟loaded()
功能 - 只需这样做:
window.addEventListener('load', loaded, false);
如果您正在假设W3C DOM事件处理(您的load
处理程序执行),那么就不需要进行IE-ony attachEvent()
调用