我想捕捉从下拉菜单中选择的选项
问题在于点击option
时TR
点击事件被调用,因为select没有点击事件。
如果点击TR
,我该如何停止dropdown
点击事件?
function check1(e) {
if (typeof e == "undefined")
e = window.event;
if (e) {
var sourceElement = e.srcElement || e.target;
if (sourceElement) {
if (sourceElement.getAttribute("data-stop-propagation")) {
//element being clicked asked to ignore the event, abort
return;
}
}
}
alert("TR clicked");
}
function check2() {
alert("drop_down clicked");
}

<table border=1>
<tr onclick="check1();">
<td>My Options</td>
<td>
<select name="" id="drop_down" onchange="check2();" data-stop-propagation="1">
<option value="A">A</option>
<option value="B">B</option>
</select>
</td>
</tr>
</table>
&#13;
答案 0 :(得分:2)
稍微回过头来,select.click
在tr.click
事件之前执行select.change
之前执行。为什么不在单击选择时停止传播?
function check1() {
alert("TR clicked");
}
function check2() {
alert("drop_down clicked");
}
function stopPropagation() {
event.stopPropagation();
};
&#13;
<table border=1>
<tr onclick="check1();">
<td>My Options</td>
<td>
<select name="" id="drop_down" onclick="stopPropagation();" onchange="check2();" data-stop-propagation="1">
<option value="A">A</option>
<option value="B">B</option>
</select>
</td>
</tr>
</table>
&#13;