首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

javascript問題解决办法

2012-02-17 
javascript問題1.Thereisanotherconceptrelatedtousingreturnfalseinaneventhandler.Ifyoufullyhandleagiv

javascript問題
1.There   is   another   concept   related   to   using   return   false   in   an   event   handler.   If   you   fully   handle   a   given
event   in   a   contained   control,   you   may   not   want   that   same   event   to   “bubble   up”   to   any   parent   controls   it
is   within.   For   instance   in   the   previous   example,   you   may   not   want   the   onclick   event   on   the   <a>   to   be
seen   by   an   onclick   handler   for   the   <body>   element.   But   by   virtue   of   <a>   being   a   child   element   under
<body> ,   normally   this   onclick   would   bubble   up.   If   you   use   only   return   false,   then   it   will   defer
what   would   happen   on   the   <a>   but   not   what   would   happen   in   an   onclick   on   the   <body> .   To   keep   this
from   happening,   use   this   statement:

event.cancelBubble=true;

You   must   use   this   statement   before   any   return   because   executing   return   indicates   that   you’re   done
with   the   event   handler   code   and   want   to   return   control   to   the   browser.
如果我有100個button需要寫alert( "* ");
event.cancelBubble=true;是沈麼意思,可以實現嗎?
2.
You   don’t   always   want   script   to   be   executed   as   the   page   is   loaded.   Quite   often   you   want   script   executed   in
response   to   events   such   as   a   user   clicking   a   particular   screen   element   or   a   mouse   movement.   (For   example,
you   may   recall   that   in   Chapter   2,   you   used   the   onload   event   to   insert   custom   text   into   the   status
area   of   a   browser   window   once   a   page   had   fully   rendered.)   This   activity   is   usually   done   by   referencing
either   inline   JavaScript   or   a   function   using   an   XHTML   attribute.   In   the   following   examples,   you   are   handling
the   onclick   event   of   a   button   first   using   inline   script:
<input   type=”button”   value=”Click   Me   (inline)”   onclick=”alert(‘I   was   clicked   -
inline   script’);”   />
and   then   using   a   HandleClick()   function:
<input   type=”button”   value=”Click   Me   (function)”   onclick=”HandleClick();”   />
The   onclick   event   is   probably   one   of   the   most   familiar   events   to   web   developers   and   doesn’t   have   to   be
applied   only   to   obvious   items   such   as   buttons.   It   can   be   applied   to   other   items   such   as   paragraph   tags,


<p> ,   and   most   other   items   as   well.
<p   onclick=”HandleParaTagClick();”> This   is   some   paragraph   text </p>

inline   HandleParaTagClick()都是甚麼?
為甚麼教科書里這麼寫,沒有定義的函數呀!


[解决办法]
event.cancelBubble=true; 这是IE实现的事件机制, 防止事件向上传播.
比如你在td中点击了一下,那么正常情况下,事件会向上传播,tr会得到onclick事件,table也会,body会,最后,document也会得到. 设置event.cancelBubble=true;后, 事件不向上传播了

HandleParaTagClick()是自定义的函数,看不到不代表没有

热点排行