为什么在 onclick 事件处理程序中使用 return?

Don*_*Don 2 javascript

为什么在下面的 onclick 事件处理程序中使用“return”,似乎在按钮的 onclick 事件中有或没有返回规范的情况下都起作用?

input type="radio" name="radCPUSpeed" value="6 Ghz"
                       onclick="return radCPUSpeed_onclick(2)"
Run Code Online (Sandbox Code Playgroud)

该函数似乎没有做任何事情,为什么上面的输入声明中需要 return ?

function radCPUSpeed_onclick(radIndex)
   {
       var returnValue = true;
       if (radIndex == 1)
       {
           returnValue = false;
           alert("Sorry that processor speed is currently unavailable");
           // Next line works around a bug in IE that doesn't cancel the
           // Default action properly
           document.form1.radCPUSpeed[radCpuSpeedIndex].checked = true;
       }
       else
       {
           radCpuSpeedIndex = radIndex;
       }
       return returnValue;
   }
Run Code Online (Sandbox Code Playgroud)

Tim*_*own 5

false从事件处理函数返回将阻止与该事件关联的默认浏览器行为。返回任何其他值(包括undefined,如果没有return执行语句则返回的值)将不会。重要的是,返回false不会阻止事件在 DOM 树中向上冒泡,并且附加到 DOM 树较高节点的事件处理程序仍将被执行。

另请注意,这不适用于通过addEventListener或附加的事件侦听器函数attachEvent。此类函数中的返回值没有任何作用。