Nová ukázka
Reset
Načíst z URL
Uložit a získat odkaz
Vztahuje se k…
Nevyplňujte
Napiště „nejsem robot“
▶
Přepnout zobrazení
Výsledek
<form> <p> <label for="email">E-mail</label> <br> <span class="emailHint"> <input id="email" name="email" type="text" value="email@"> <span class="emailHint-hint"></span> </span> </p> <p> <label for="password">Password</label> <br> <input id="password" type="password"> </p> <p> <button>Send</button> </p> </form> <script> EmailHint.init( "emailHint", // className [ "seznam.cz", "gmail.com", "hotmail.com", "centrum.cz", "email.cz", "atlas.cz", "azet.sk", "volny.cz", "centrum.sk", "post.cz", "outlook.com", "zoznam.sk", "yahoo.com", "tiscali.cz", "post.sk", "quick.cz", "aol.com", "pobox.sk", "szm.sk", "atlas.sk" ] ); </script>
HTML
Autoformát
Standardní režim
Mobilní zobrazení
input, .emailHint-measure, .emailHint-hint { font-family: "Segoe UI", sans-serif; font-size: 100%; border: 1px solid gray; padding: .2em; } .emailHint { position: relative; } .emailHint-measure { padding-right: 0; border-right: 0; position: absolute; top: -999em; visibility: hidden; } .emailHint-hint { color: gray; position: absolute; padding-left: 0; border-left: 0; border-color: transparent; }
CSS
Autoformát
CSS reset
Až na konci
var EmailHint = function() { var domains; var measureText = function(el) { var span = document.createElement("span"); span.innerHTML = el.value; span.className = "emailHint-measure"; document.body.appendChild(span); var width = span.offsetWidth; document.body.removeChild(span); return width; }; var addHint = function(el) { el.parentNode.getElementsByTagName("input")[0].value += el.innerHTML; el.innerHTML = ""; }; var findHint = function(hint, el) { var domainsLength = domains.length; for (var i = 0; i < domainsLength; i++) { var hintElement = el.parentNode.getElementsByTagName("span")[0]; if (hint.length > 0 && domains[i].indexOf(hint) === 0) { hintElement.innerHTML = domains[i].replace(hint, ""); hintElement.style.left = measureText(el) + "px"; break; } else { hintElement.innerHTML = ""; } } }; var removeHint = function(el) { var hintElement = el.parentNode.getElementsByTagName("span")[0]; hintElement.innerHTML = ""; }; var showHint = function(el) { var at = el.value.indexOf("@"); removeHint(el); if (at !== -1) { var hint = (el.value.substr(at + 1, el.value.length)); if (hint) { findHint(hint, el); } } }; var init = function(className, hints) { domains = hints; var emailElements = document.querySelectorAll("." + className); for (var i = emailElements.length - 1; i >= 0; i--) { var input = emailElements[i].getElementsByTagName("input")[0]; input.addEventListener("keyup", function() { showHint(this); }, false); input.addEventListener("blur", function() { addHint(this.nextElementSibling); }, false); var span = emailElements[i].getElementsByTagName("span")[0]; span.addEventListener("click", function() { addHint(this); }, false); input.form.addEventListener("submit", function() { addHint(span); }, false); }; }; return { init: init } }();
J
ava
S
cript
Autoformát
jQuery
Umístění JS
window.onload
</head>
</body>