JavaScript addEvent函数
发布网友
发布时间:2022-04-25 22:21
我来回答
共3个回答
热心网友
时间:2022-04-25 23:50
element["on" + type] = handleEvent
中element["on" + type], element是传进来的标签对象 type就是传入的事件名如 click等
举个例子 type 为click,那合起来意思等同 element.onclik=handleEvent,其中handleEvent就是
事件触发调用的函数。
理解这种写法就要理解 对象的j定义
如 var obj={a:123}如果要取obj对象的123这个值,那有2种方法取
1 --用点号 obj.a
2 -- 用【】 obj【“a”】上面程序就是用的这种形式.
热心网友
时间:2022-04-26 01:08
if(typeof window.addEventListener==="function")
{
alert("DOM浏览器");
}
else
{
alert("IE");
}
高手写的一个检测浏览器的代码
var isIE = !!(window.attachEvent && !window.opera);
var isOpera = !!window.opera;
var isSafari = navigator.userAgent.indexOf(’AppleWebKit/’) > -1;
var isMoz = navigator.userAgent.indexOf(’Gecko’) > -1 && navigator.userAgent.indexOf(’KHTML’) == -1;
var isMobileSafari = !!navigator.userAgent.match(/Apple.*Mobile.*Safari/);
var isIE5 = (navigator.appVersion.indexOf("MSIE 5")>0) || (navigator.appVersion.indexOf("MSIE")>0 && parseInt(navigator.appVersion)> 4);
var isIE55 = (navigator.appVersion.indexOf("MSIE 5.5")>0);
var isIE6 = (navigator.appVersion.indexOf("MSIE 6")>0);
var isIE7 = (navigator.appVersion.indexOf("MSIE 7")>0);
var isIE8 = (navigator.appVersion.indexOf("MSIE 8")>0);
attachEvent方法,为某一事件附加其它的处理事件。(不支持Mozilla系列)
addEventListener方法 用于 Mozilla系列
document.getElementById("btn").onclick = method1;
document.getElementById("btn").onclick = method2;
document.getElementById("btn").onclick = method3;
Java代码
if (window.addEventListener)
{
window.addEventListener('load', _uCO, false);
}
else if (window.attachEvent)
{
window.attachEvent('onload', _uCO);
}
var handler = function()
{
var host = document.location.href;
if(host.indexOf('jx163.cn') >= 0)
document.location = "/";
else ;
};
// 添加监听
if (window.addEventListener)
{
window.addEventListener('DOMContentLoaded', handler, false);
window.addEventListener('load', handler, false);
}
else if (window.attachEvent)
window.attachEvent('onload', handler);
else
window.onload = handler;
}
init();
</script>
</head>
<body>
--------------------
window和firefox下的event
--------------------
<SCRIPT LANGUAGE="JavaScript">
<!--
function hh(e)
{
alert(arguments[0]);
e=window.event||e;
var el=e.srcElement||e.target;
alert(el.value);
}
//-->
</SCRIPT>
<INPUT id ="button2" value ="hahahahah" TYPE="button" onclick="hh(event);">
--------------------
test
--------------------
if(WebComm.getOS() == "MSIE")
{
obj.setCapture ();
}
else
{
document.addEventListener("mousemove",WebComm.MoveDiv,false);
}
if(WebComm.getOS() == "MSIE")
{
obj.releaseCapture ();
}
else
{
document.removeEventListener("mousemove",WebComm.MoveDiv,false);
}
热心网友
时间:2022-04-26 02:43
a) function addEvent(el, type, fn){
if(el.attachEvent){
el['e’+type+fn] = fn;
el[type+fn] = function(){
el[‘e’+type+fn](window.event);
}
el.attachEvent(‘on’+type, el[type+fn]);
}
else
el.addEventListener(type, fn, false);
}
addEvent(window, 'load', init);
function init() {
var odiv = document.getElementByIdx_x("divtest");
addEvent(odiv, 'onmouseover', onmousefunc);
addEvent(odiv, 'onclick', onclickfunc);
addEvent(odiv, 'onmouseout', mouseoutfunc);
}
function onmousefunc(){ ... }
function onclickfunc(){ ... }