问题
This works in Firefox, but not IE. Any help would be much appreciated! Thanks!
var form = document.getElementById('theform')
/* create the event handler */
form.gen.onclick = function( evt ) {
var f = evt.target.form
var y = f.year.value
var m = f.month.value
genCalendar( document, y, m, 'theCalendar' )
}
回答1:
To get the target of an event in both standards compliant browsers and IE, use
var target = evt ? evt.target : window.event.srcElement;
There's an overview of the different properties of event objects at MDC.
回答2:
As mentioned, IE does not pass the event object as a parameter. Try this:
var form = document.getElementById('theform')
/* create the event handler */
form.gen.onclick = function( evt ) {
if(!evt)
evt = window.event;
var f = evt.target.form
var y = f.year.value
var m = f.month.value
genCalendar( document, y, m, 'theCalendar' )
}
Or better yet, use a cross-browser library, like Prototype.js or jQuery.
回答3:
- When does this script run? You might have to run this script onload, after the DOM is fully loaded
<script> function go() { alert('dom is loaded: register event handlers now') ; } </script> <body onload=" go(); "> </body>
回答4:
This is why you should consider using a javascript library such as jquery, YUI, or prototype. These libraries abstract away the browser based differences which simplifies your coding.
回答5:
From my own searching the most sucessful was this
function clickHandler(e){
var elem, evt = e ? e:event;
if (evt.srcElement) elem = evt.srcElement;
else if (evt.target) elem = evt.target;
alert (''
+'You clicked the following HTML element: \n <'
+elem.tagName.toUpperCase()
+'>'
)
return true;
}
document.onclick=clickHandler;
Sourced from ther very helpful and explanatory http://www.javascripter.net/faq/eventtargetsrcelement.htm
来源:https://stackoverflow.com/questions/960419/javascript-ie-event