Created
July 30, 2017 11:27
-
-
Save webbingstudio/b962ad96a0a4b6803a298e35d95ee418 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
基本的な使い方
<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>