Skip to content

Instantly share code, notes, and snippets.

@webbingstudio
Created July 30, 2017 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webbingstudio/b962ad96a0a4b6803a298e35d95ee418 to your computer and use it in GitHub Desktop.
Save webbingstudio/b962ad96a0a4b6803a298e35d95ee418 to your computer and use it in GitHub Desktop.
;(function($){
"use strict";
var Mailto = window.Mailto || {};
Mailto = (function() {
function Mailto(element, options) {
var t = this;
t.initials = {
data_set: 'mailto-set',
data_changetxt: 'mailto-changetxt',
split: '***'
};
t.settings = $.extend({}, t.initials, options);
t.run(element);
return false;
}
return Mailto;
}());
Mailto.prototype.run = function(element) {
var
s,
m,
t = this,
e = $(element);
s = e.data(t.settings.data_set).split(t.settings.split);
if (s.length === 3) {
m = s[1] + '@' + s[0] + s[2];
if (e.prop('href') !== undefined) {
e.attr('href', 'mailto:' + m);
}
if (e.data(t.settings.data_changetxt) === true) {
e.text(m);
}
}
};
$.fn.Mailto = function() {
var
element = this,
options = arguments[0],
args = Array.prototype.slice.call(arguments, 1),
i;
for (i = 0; i < element.length; i++) {
if (typeof options == 'object' || typeof options == 'undefined') {
element[i].Mailto = new Mailto(element[i], options);
}
}
return element;
};
// activate
// -------------------------
$(function(){
$('.js-mailto').Mailto();
});
})(jQuery);
@webbingstudio
Copy link
Author

webbingstudio commented Jul 30, 2017

基本的な使い方
<a href="#" class="js-mailto" data-mailto-set="(@より後の前半)***(@より前)***(@より後の後半)">メールを送る</a>

hrefだけでなくテキストもメールアドレスに置き換える
<a href="#" class="js-mailto" data-mailto-set="(@より後の前半)***(@より前)***(@より後の後半)" data-mailto-changetxt="true">メール</a>

span要素やp要素にも使えます
<span class="js-mailto" data-mailto-set="(@より後の前半)***(@より前)***(@より後の後半)" data-mailto-changetxt="true">メール</span>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment