【问题标题】:Prevent Default Not working Jscript if(confirm){}else{prevent default}阻止默认 Javascript if(confirm){}else{preventdefault} 不起作用
【发布时间】:2018-06-05 22:06:44
【问题描述】:

我确实让这个工作正常,但现在我改变了我的 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;
}

};

【问题讨论】:

    标签: php html css jscript confirm


    【解决方案1】:
    1. 如果要使用 event.preventDefault(),则需要将事件对象传递给 facebookalert 函数。

    2. event.preventDefault();不是您想要用来阻止该窗口打开的东西。

    改为这样使用

    <?
        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() 方法阻止元素的默认操作发生。

    例如:

    防止提交按钮提交表单。

    阻止链接跟随 URL。

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

    【讨论】:

    • 谢谢你,非常感谢。那工作得很好......我真的要弄清楚所有这些东西......大声笑
    • 乐于助人。如果它解决了您的问题,您可以接受答案。
    • 所以如果 (facebookalert( 即返回 true )){ 然后继续使用 window.open 调用}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 2010-09-25
    相关资源
    最近更新 更多