-4

我有几个带有电子邮件图标的 div。

<div class="icon-send-mail" id="test1@email.com"></div>
<div class="icon-send-mail" id="test2@email.com"></div>
<div class="icon-send-mail" id="test3@email.com"></div>

我想在用户单击该图标时发送电子邮件。

$( document ).ready( function() {
    $('.icon-send-mail').click(function(email) {
      var mailto_link = 'mailto:'+email;
      var win = window.open(mailto_link,'emailWindow');
    });
});

如何才能做到这一点?

4

5 回答 5

2

编辑函数中的第一行:

/* You were passing the event object! */
var mailto_link = 'mailto:'+this.id;

自定义 JQuery 与 JavaScript 性能基准测试

JavaScript/JQuery

$(document).ready(function(){
    $('.icon-send-mail').click(function(event) {
      var mailto_link = 'mailto:'+this.id;
      var win = window.open(mailto_link,'emailWindow');
    });
});

纯Javascript

window.onload = function () {
    var elems = document.getElementsByClassName('icon-send-mail');
    for (var i in elems) {
        if (elems[i].nodeType == 1) elems[i].addEventListener('click', function (event) {
            var mailto_link = 'mailto:' + this.id;
            var win = window.open(mailto_link, 'emailWindow');
        });
    }
};

奖金 CSS

.icon-send-mail {
    cursor: pointer;
}

现场演示

于 2013-07-31T13:26:43.563 回答
2

点击处理程序的输入是一个事件对象。在该事件对象中,您可以获得被点击的元素,因此它是 id,在您的情况下,它包含电子邮件地址

$('.icon-send-mail').click(function(event) {
  var mailto_link = 'mailto:'+event.target.id;
  var win = window.open(mailto_link,'emailWindow');
});
于 2013-07-31T13:28:30.870 回答
1

你在正确的轨道上:

$( document ).ready( function() {
    $('.icon-send-mail').on('click', function(e) {
        var mailto_link = 'mailto:' + $(this).attr('id');
        var win = window.open(mailto_link, 'emailWindow');
    });
});
于 2013-07-31T13:24:38.863 回答
1

你应该替换你的这一行:

var mailto_link = 'mailto:'+email;

使用以下行:

var mailto_link = 'mailto:'+($(this).attr('id'));
于 2013-07-31T13:26:02.493 回答
0

嗨,在您的 Click 事件中调用下面的代码

$.ajax({
       type: "POST",
       url: "Mail.aspx/SendMail",
       cache: false,
       contentType: "application/json; charset=utf-8",
       data: "{ 'body':'"  + messageBody + "'," +
                 "'to': '" + msgTo + "'," +
                 "'from': '" + msgFrom + "'," +
                 "'subject': " + msgSubject + "'" +
             "}",
       dataType: "json",
       complete: function (transport) {  if (transport.status == 200) $("#formcontainer").html("Success"); else alert("Please try again later"); }
   });

并在后面的代码上写邮件代码

MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

            mail.From = new MailAddress("me@mydomain.com");
            mail.To.Add("u@urdomain.com");
            mail.Subject = filename;
            mail.Body = "Report";
            Attachment attachment = new Attachment(filename);
            mail.Attachments.Add(attachment);

            SmtpServer.Port = 25;
            SmtpServer.Credentials = new System.Net.NetworkCredential("me", "password");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);
于 2013-07-31T13:24:41.363 回答