<button onclick="fun()">click</button>
<button onclick="show()">show</button>
<script type="text/javascript">
function fun()
{
//firefox可以
/*var div = document.createElement("div");
var str = '<script type="text/javascript">\
function show()\
{alert("good")}\
<\/script>'
div.innerHTML = str;
document.body.appendChild(div);*/
var spt = document.createElement("script");
spt.type = "text\/javascript";
//spt.innerHTML = "function show(){alert(123)}";//firefox下可以
spt.src = "js.js";//IE和firefox都可以
document.body.appendChild(spt);
}
</script>
<script type="text/javascript">
function fun()
{
var btn = document.getElementById("btn");
if(btn.onclick)
alert("yes");
else
alert("no");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>事件检测</title>
<script>
function fun(){
var str=document.test.b1.onclick.toString();
alert(str);
if(str.lastIndexOf("fun()")>0) alert("事件是fun()");
}
</script>
</head>
<body>
<form name="test">
<input id="b1" type="button" value="点击" onclick="fun()" />
</form>
</body>
</html>这个onclick.toString()在不同的浏览器中有不同的值,但只要检测有没有包含目标函数名在内就可以了
比如 document.getElementById("b1").onclick=fun;
或直接写匿名事件函数
document.getElementById("b1").onclick=function(){alert("OK");}
<!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>
function fun(){
var str=document.test.b1.onmouseover;
if(str !=null || str!=undefined)//兼容FF
{
alert("事件是fun()");
}
else
{
alert("不存在该事件");
}
//if(str.lastIndexOf("fun()")>0) ;
}
</script>
</head>
<body>
<form name="test">
<input id="b1" type="button" value="点击" onclick="fun()" />
</form>
</body>
</html>