Javascript 带有锚元素的jQuery mailto

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26104378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:21:24  来源:igfitidea点击:

jQuery mailto with anchor element

javascriptjqueryhtmlanchormailto

提问by Kevin Rave

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:

查询:

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

Granted, I am a jQuery newbie. The above just doesn't work. It just flickers the browser but nothing opens. I guess this is because of window.location.

当然,我是 jQuery 新手。以上只是行不通。它只是在浏览器中闪烁,但什么也没有打开。我猜这是因为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 中)。

回答by krozero

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 = '[email protected]';
    var subject = 'Circle Around';
    var emailBody = 'Some blah';
    window.location = 'mailto:' + email + '?subject=' + subject + '&body=' +   emailBody;
  });
});

回答by Cheery

$(function () {
  $('[name=emailLink]').click(function () {
    var email = '[email protected]';
    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 = '[email protected]';
    var subject = 'Circle Around';
    var emailBody = 'Some blah';
    $(this).attr('href', 'mailto:' + email +
           '?subject=' + subject + '&body=' +   emailBody);
  });
});

回答by rakete

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!

机器人没有机会!

回答by nickles80

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"到锚标记。

回答by Dave

You don't need any javascript/jQuery at all for this, just the following HTML should do:

为此,您根本不需要任何 javascript/jQuery,只需以下 HTML 即可:

<a href="mailto:[email protected]?subject=Circle Around&body=Some blah" name="emailLink">Email</a>