Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to use "attachEvent"?;

4 views
Skip to first unread message

SkyZhao

unread,
Apr 24, 2007, 10:00:23 PM4/24/07
to
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?

Randy Webb

unread,
Apr 24, 2007, 10:36:23 PM4/24/07
to
SkyZhao said the following on 4/24/2007 10:00 PM:

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

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

never did care for attachEvent.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Peter Michaux

unread,
Apr 26, 2007, 1:56:06 AM4/26/07
to
On Apr 24, 7:00 pm, SkyZhao <sky.zhao....@gmail.com> wrote:
> 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

Peter Michaux

unread,
Apr 26, 2007, 1:59:09 AM4/26/07
to
On Apr 24, 7:36 pm, Randy Webb <HikksNotAtH...@aol.com> wrote:
> SkyZhao said the following on 4/24/2007 10:00 PM:
>
> > i use attachEvent to bind a event ;
> > code:
> > img.attachEvent("onclick",alert("aa"));
> > div.appendChild(img);
> > div.innerHTML+="some text";
>
> 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

0 new messages