【问题标题】:Mailto link with JqueryMailto 与 Jquery 的链接
【发布时间】:2023-03-19 07:45:01
【问题描述】:

我的网站上有一个可以正常工作的 mailto 链接。但是,我想在用户单击该链接时对其执行 .click() 事件以进行记录。我有 .click 事件工作并执行 ajax 请求,但现在 mailto 链接没有打开电子邮件客户端。是否可以在客户端打开之前运行jquery函数,但仍然让客户端打开?

这是我的代码(它只是在浏览器中打开一个空白窗口)

<script type='text/javascript'>
                    jQuery('span.email a').on('click',function (){
                        jQuery.ajax({
                            type: 'POST',
                            url: '', //url here
                            data: {comment: 'Email Sent', category : 'EE', store_id: '" . $this->store_id . "'},
                            success: function (data){jQuery('#alerts').html(data);}
                        });
                    window.location.href= $(this).prop('href');
                });
                </script>

【问题讨论】:

    标签: jquery html mailto


    【解决方案1】:

    您不需要window.location.href= $(this).prop('href');。如果您只是让单击继续正常进行,它应该会执行您想要的操作。

    【讨论】:

    • 当我这样做时,ajax 请求发生得很好,但客户端没有打开。但是,当我删除 onclick 事件时,客户端会打开。
    • 尝试使用 ajaxed 对象传递 asyc: true
    • 没关系,我刚刚从 Outlook 2010 降级到 2003,现在 mailto 链接根本不起作用。我有一个朋友试了一下,它工作正常。感谢您的帮助。
    • 我的荣幸。如果您发现这回答了您的问题,请将其标记为已回答:)
    【解决方案2】:

    为了使其正常工作,您需要将 async 设置为 false。

    http://jsfiddle.net/5nwu7/3/

    <script type='text/javascript'>
                    jQuery('span.email a').on('click',function (){
                        jQuery.ajax({
                            type: 'POST',
                            url: '', //url here
                            data: {comment: 'Email Sent', category : 'EE', store_id: '" . $this->store_id . "'},
                            success: function (data){jQuery('#alerts').html(data);},
                            async: false
                        });
                    //You don't need this ---window.location.href= $(this).prop('href');
                });
                </script>
    

    这是因为某些浏览器会在打开邮件客户端时取消你当前的 ajax 请求。您可以通过转到我发布的 jsfiddle 并删除异步行来测试这一点(在 chrome 23.0.1271.97 m、windows 7、outlook 2007 中测试。)

    【讨论】:

      猜你喜欢
      • 2011-06-10
      • 1970-01-01
      • 2011-04-21
      • 2012-09-17
      • 2011-10-26
      • 2012-10-25
      • 2021-08-03
      • 2018-10-26
      • 2015-01-23
      相关资源
      最近更新 更多