首页 > 解决方案 > 单击特定项目时执行js函数

问题描述

请告诉我go2当我单击按钮而不执行该功能时如何执行该go功能

function go($i) {
  alert($i);
}
function go2($i) {
  alert($i);
}
div {
  width: 200px;
  height: 200px;
  background: #eaeaea;
}
<div onclick="go('159');">
<button onclick="go2('160');">Go 2</button>
</div>

标签: javascripthtml

解决方案


你需要使用 event.stopPropagation()

function go($i) {
  alert($i);
}
function go2($i) {
  event.stopPropagation();
  alert($i);
}
div {
  width: 200px;
  height: 200px;
  background: #eaeaea;
}
<div onclick="go('159');">
<button onclick="go2('160');">Go 2</button>
</div>


推荐阅读