javascript - 我在按钮中有图像,按钮和图像都有单独的 onclick 功能,当我在按钮上单击此图像时,它也会在按钮上执行功能
问题描述
这是按钮。想象一下带有文本 B2C01 和旁边的 svg 的按钮,它的工作方式很接近。当我单击此图像时,按钮会消失,但同时会触发执行其他操作的按钮功能,并且会干扰我的代码。单击图像时如何阻止它执行?
<button onclick="clickcustomer(this.id)"
id="addedprivatecustomerbutton1"
class="addedprivatecustomerbutton displaynone" type="button" name="button">B2C01
<img class="removeaddedcustomerbutton" id="addedprivatecustomersvgsvg1"
src="img\removeaddedcustomerbutton.svg"
alt="" onclick="removeprivatecustomer(this.id)"> </button>
解决方案
您的 HTML 将是,
<button onclick="clickcustomer()"
id="addedprivatecustomerbutton1"
class="addedprivatecustomerbutton displaynone" type="button" name="button">B2C01
<img class="removeaddedcustomerbutton" id="addedprivatecustomersvgsvg1"
src="img\removeaddedcustomerbutton.svg"
alt="" onclick="removeprivatecustomer(event)"> </button>
JS会是,
removeprivatecustomer(e){
console.log("removeprivatecustomer")
e.stopPropagation();
console.log("removeprivatecustomer after stopPropagation")
}
clickcustomer(){
console.log("clickcustomer")
}
推荐阅读
- sql - 显示应用于多列的公式结果
- java - android studio中PendingIntent中的请求代码是什么
- git - 每次我合并回它时都会引发冲突
- python - 如何迭代熊猫数据框并检查日期时间索引中的日期
- ruby-on-rails - Rails 查询未执行
- image-processing - LFW 数据集如何用于评估 facenet 模型
- qt - QString.toInt() 不起作用 - 错误:二进制表达式的操作数无效('const char *' 和 'const char [14]')
- python - 如何在给定的不规则日期重新采样时间序列
- typescript - 是否可以用玩笑递归地遍历对象?
- kubernetes - 使用 celery 工作者在 kubernetes 上部署后的气流 ui 日志错误