实现带确认按钮的Toastr时遇SyntaxError: missing ) after argument list问题
修复Toastr确认弹窗的语法错误
嘿,我帮你排查下这个问题~你遇到的SyntaxError: missing ) after argument list,大概率是代码里的字符串拼接没做完、括号/引号没闭合,或者调用toastr的方式不对。从你给的代码片段来看,主要问题集中在这几个地方:
先说说出错的核心原因
- 你写的HTML字符串中途截断了(比如
cla...明显是class没写完,而且标签也没闭合) - 调用
toastr.warning的时候,参数没凑齐、括号没闭合 - 原函数名
detele是拼写错误,而且delete是JS关键字,最好换个名字避免冲突
给你写了完整的修复代码,直接用就行
<script type="text/javascript"> // 换个函数名,避免和关键字冲突 function confirmDelete() { // 用模板字符串写HTML,比加号拼接清爽多了,还不会乱引号 const confirmContent = ` <div>confirmer la suppression?</div> <div class='btn-group pull-right'> <button type='button' id='confirmYes' class='btn btn-xs btn-primary'> <i class='glyphicon glyphicon-ok'></i> Oui </button> <button type='button' id='confirmNo' class='btn btn-xs btn-default'> <i class='glyphicon glyphicon-remove'></i> Non </button> </div> `; // 调用toastr.warning,注意参数要完整,还要开allowHtml才能解析HTML toastr.warning(confirmContent, 'Confirmation', { closeButton: false, allowHtml: true, // 这个必须开!不然HTML会变成纯文本 onShown: function() { // 弹窗显示后再绑事件,确保按钮已经在DOM里 document.getElementById('confirmYes').addEventListener('click', function() { // 这里写你确认删除的逻辑 console.log("Suppression validée !"); toastr.clear(); // 删完关掉弹窗 }); document.getElementById('confirmNo').addEventListener('click', function() { toastr.clear(); // 取消就直接关弹窗 }); }, onHidden: function() { // 弹窗关了之后清掉事件绑定,避免重复点击触发多次 const yesBtn = document.getElementById('confirmYes'); const noBtn = document.getElementById('confirmNo'); if (yesBtn) yesBtn.removeEventListener('click', function(){}); if (noBtn) noBtn.removeEventListener('click', function(){}); } }); } </script>
几个关键的修复细节
- 模板字符串:用
`包裹HTML内容,不用再费劲拼接字符串,还能避免引号冲突 - Toastr配置:一定要加
allowHtml: true,不然Toastr会把你的按钮代码当成纯文字显示 - 事件时机:在
onShown里绑按钮事件,因为弹窗没显示的时候,按钮还没被渲染到页面上,直接绑会找不到元素 - 括号闭合:确保
toastr.warning的三个参数(内容、标题、配置对象)都被正确的括号包裹,这就是解决missing )错误的关键
内容的提问来源于stack exchange,提问作者Rev Damage
相关产品推荐
相关产品推荐

