Javascript在下拉列表选择中停止TR单击事件在IE 11中不起作用

时间:2017-12-14 09:38:58

标签: javascript event-bubbling

我想捕捉从下拉菜单中选择的选项 问题在于点击optionTR点击事件被调用,因为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;
&#13;
&#13;

[1]: https://jsfiddle.net/oqgdh59o/3/

1 个答案:

答案 0 :(得分:2)

稍微回过头来,select.clicktr.click事件之前执行select.change之前执行。为什么不在单击选择时停止传播?

&#13;
&#13;
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;
&#13;
&#13;