how to use "attachEvent"?;

S

SkyZhao

i use attachEvent to bind a event ;
code:
img.attachEvent("onclick",alert("aa"));
div.appendChild(img);
div.innerHTML+="some text";

the event can not work; why can't i use it?

if i use nether code , it can work;

code:
img.attachEvent("onclick",alert("aa"));
div.appendChild(img);
spanWord = document.createElement("span");
spanWord.innerHTML="some text";
div.appendChild(spanWord);

do anybody know the reason?
 
P

Peter Michaux

i use attachEvent to bind a event ;
code:
img.attachEvent("onclick",alert("aa"));

Above you have used a function call as the second argument attachEvent

The second argument to attachEvent is a function...

img.attachEvent("onclick",function(){alert{"aa");});
div.appendChild(img);
div.innerHTML+="some text";

Peter
 
P

Peter Michaux

SkyZhao said the following on 4/24/2007 10:00 PM:


img.onclick= function(){alert('aa')}

never did care for attachEvent.

Loose coupling between chunks of JavaScript and event delegation must
not be your thing, eh? It is relatively frequently that I attach more
than one window.onload handler to initialize javascript pieces that
deal with different parts of the page.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top