简体   繁体   English

带锚元素的jQuery mailto

[英]jQuery mailto with anchor element

I tried this with umpteen examples we see on the net. 我用网上看到的无数例子尝试了这个。 But I guess there is none that is simple and works on all browsers (IE 8 and above as well). 但我想没有一个很简单,适用于所有浏览器(IE 8及以上版本)。

I am trying to simply open up Outlook window with mailto link. 我试图用mailto链接打开Outlook窗口。

<a href="#" name="emailLink" id="emailLink">Email</a>

JQuery: JQuery的:

$(function () {
  $('#emailLink').on('click', function (event) {
    alert("Huh");
    var email = 'test@theearth.com';
    var subject = 'Circle Around';
    var emailBody = 'Some blah';
    window.location = 'mailto:' + email + '?subject=' + subject + '&body=' +   emailBody;
  });
});

Granted, I am a jQuery newbie. 当然,我是一个jQuery新手。 The above just doesn't work. 以上只是不起作用。 It just flickers the browser but nothing opens. 它只是闪烁浏览器但没有打开。 I guess this is because of window.location . 我想这是因为window.location

Is there a simple solution? 有简单的解决方案吗? I want this to work in IE8 & above and in all browsers. 我希望这可以在IE8及以上版本和所有浏览器中使用。

The body is generated automatically (in JSP). 正文自动生成(在JSP中)。

here's working solution: 这是工作解决方案:

<a href="#" name="emailLink" id="emailLink">Email</a>

and the function: 和功能:

$(function () {
  $('#emailLink').on('click', function (event) {
      event.preventDefault();
    alert("Huh");
    var email = 'test@theearth.com';
    var subject = 'Circle Around';
    var emailBody = 'Some blah';
    window.location = 'mailto:' + email + '?subject=' + subject + '&body=' +   emailBody;
  });
});
$(function () {
  $('[name=emailLink]').click(function () {
    var email = 'test@theearth.com';
    var subject = 'Circle Around';
    var emailBody = 'Some blah';
    $(this).attr('href', 'mailto:' + email +
           '?subject=' + subject + '&body=' +   emailBody);
  });
});

.click can be replaced with .mousedown and so on.. or just .click可以替换为.mousedown等等。或者只是

$(function () {
  $('[name=emailLink]').each(function() {
    var email = 'test@theearth.com';
    var subject = 'Circle Around';
    var emailBody = 'Some blah';
    $(this).attr('href', 'mailto:' + email +
           '?subject=' + subject + '&body=' +   emailBody);
  });
});

If you do not need the address as a text anywhere on the website I would suggest this: 如果您不需要在网站上的任何地方作为文本地址我建议:

$('a[data-mail]').on('click', function() {
   window.location = 'mailto:' + $(this).data('mail')+'@yourdomain.net' + '?subject=Spotflow';
});

The link woud look like this: 链接看起来像这样:

<a href="#" data-mail="max">Send me a mail</a>

No chance for bots! 没机会机器人!

Your selector is looking for an ID 您的选择器正在寻找ID

$('#emailLink')

But you have only specified the name. 但是你只指定了名字。

Add id="emaillink" to the anchor tag. id="emaillink"添加到锚标记。

你根本不需要任何javascript / jQuery,只需要下面的HTML:

<a href="mailto:test@theearth.com?subject=Circle Around&body=Some blah" name="emailLink">Email</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM