我最近調netsurf也遇到一個相關的bug : alert() 被調了兩次。html 代碼:
復制代碼 代碼如下:
<html>
<head>
<title>alert onclick example</title>
<script type="text/javascript">
function causealert()
{
var txt = document.getElementById("p1").textContent;
alert(txt);
}
</script>
</head>
<body>
<div style="border: 1px solid red">
<p id="p1">First line of paragraph.<br /></p>
</div><br />
<button id="button1" >add another textNode.</button>
<script>
var Button1 = document.getElementById("button1");
/*var Button1Click = function() { alert(1); };
Button1.addEventListener("click", Button1Click, false);*/
Button1.onclick = causealert;
</script>
</body>
</html>
通過gdb, 定位到是_dom_node_dispatch_event()裡面的問題:
復制代碼 代碼如下:
/* The capture phase */ for (targetnr = ntargets; targetnr > 0; --targetnr)...
/* Bubbling phase */
evt->phase = DOM_BUBBLING_PHASE;
for (targetnr = 0; targetnr < ntargets; ++targetnr)
事件流是這樣的: p1(root)-->p2-->... --> pm --> T (capturing phase), T (target phase), T--> pm --> ... --> p1 (bubbling phase).
規范規定capturing 和bubbling 只能選其一, 代碼中在 js_dom_event_add_listener()是選capturing。因此就解釋了為什麼alert 被執行兩回了。
修改: 根據DOM 3 規范,把上面的0改成1就行了。