Preventdefault 不工作 Javascript if(confirm){}else{preventdefault}

Prevent Default Not working Jscript if(confirm){}else{prevent default}

我确实让这个工作正常,但现在 iv 改变了我的 html 标记,因为我认为我缺少总和的东西,

代码解释了一切,但如果未确认,它不会阻止默认。

谁能看出是什么问题,

非常感谢任何信息。

谢谢

<?
    if (!empty($facebook)) {echo"<a href='#'onClick=\"facebookalert(); MyWindow=window.open('{$facebook}','MyWindow','width=365,height=300'); return false;\"></a>"; }

?>                                  


function facebookalert(){
if (confirm("You will be Forwarded to the sellers FACEBOOK account to make contact\nYou can return to this website as this window will not close\nThank you")) {
} else {
event.preventDefault();
     return false;
}

};
  1. 如果要使用event.preventDefault(),需要将事件对象传递给facebookalert函数。

  2. event.preventDefault();不是您想用来防止 window 打开的内容。

改为这样使用

<?
    if (!empty($facebook)) {echo"<a href='#'onClick=\"if(facebookalert()){window.open('{$facebook}','MyWindow','width=365,height=300'); return false;}\"></a>"; }

?> 

然后,只需返回 false 即可

function facebookalert(){
if (confirm("You will be Forwarded to the sellers FACEBOOK account to make contact\nYou can return to this website as this window will not close\nThank you")) {
   return true;
} else {
     return false;
}

};

来自 W3C event.preventDefault() 方法阻止元素的默认操作发生。

例如:

阻止提交按钮提交表单。

防止 link 跟随 URL。

它不会中止 window.open 的执行。