如何在行内onclick事件的函数调用中前置添加event.stopPropagation()
问题修复方案
核心错误点
- 拼写错误:DOM事件阻止冒泡的标准API正确写法是
event.stopPropagation(),错误写法StopPropogating存在拼写偏差、首字母大写不符合规范两个问题 - 行内onclick事件需要按执行顺序先写阻止冒泡逻辑,再调用自定义业务函数
修复后可运行代码
$(".select2-selection__rendered").append('<li class="select2-selection__choice" id="remove_test'+i+'" title="'+rows_value+'"><span class="select2-selection__choice__remove" onclick="event.stopPropagation();remove_e('+i+');" role="presentation">x</span>'+title_name_test+'</li> ');
补充说明
如果变量i是字符串类型而非数字类型,需要给remove_e的参数补充单引号转义,修改为remove_e(\''+i+'\')即可。
内容的提问来源于stack exchange,提问作者Bilal Hameed
相关产品推荐
相关产品推荐

