关于addEventListener问题帮别人调试一段代码,调试了一会,发现有很多问题,头都大了,还请大家帮忙!DOCTYPE
关于addEventListener问题
帮别人调试一段代码,调试了一会,发现有很多问题,头都大了,还请大家帮忙
<!DOCTYPE html>
<html>
<head>
<SCRIPT TYPE="text/javascript" SRC="jquery.js"></SCRIPT>
</head>
<body>
<a href="" title="">Long press</a>
<p>长时间点击2秒后我会变颜色</p>
<script type=text/javascript>
function down(){
var timeout = window.setTimeout(function() {$("p").css("color","yellow");},1000);
});
function up(){
$("p").css("color","black");
});
$("a").addEventListener("mousedown",down,false);
$("a").addEventListener("mouseup",up,false);
</script>
</body>
</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>无标题文档</title>
<script type="text/javascript">
var p="";
function init(){
var a=document.links[0];
if(document.body.attachEvent){
a.attachEvent("onmousedown",function(){change(1,a)});
a.attachEvent("onmouseup",function(){change(0,a)});
}else if(document.body.addEventListener){
a.addEventListener("mousedown",function(){change(1,a)},false);
a.addEventListener("mouseup",function(){change(0,a)},false);
}
}
function change(isChange,src){
if(isChange){
p=window.setTimeout(function(){src.style.color="yellow";},2000);
}else{
window.clearTimeout(p);
}
return false;
}
window.onload=init;
</script>
</head>
<body>
<a href="">按我两秒变色</a>
</body>
</html>
这样试试