Created
March 23, 2018 04:29
-
-
Save e2goon/a9074d491804c8a6cf1495e94f92d777 to your computer and use it in GitHub Desktop.
IE8~9 Placeholder
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 IEplaceholder(o) { | |
var agent = navigator.userAgent.toLowerCase(); | |
var IS_IE = agent.match(/msie\s?(\d{1,2})/g); | |
if (!IS_IE || IS_IE[0].substr(5, 6) > 9) return; | |
var $placeholders = $('[placeholder]'); | |
$placeholders | |
.wrap('<span />') | |
.each(o.addPlaceholder(o)) | |
.on('keyup', o.keyup) | |
.parent() | |
.css(o.styles.wrap) | |
; | |
}({ | |
styles: { | |
wrap: { | |
position: 'relative', | |
display: 'block' | |
}, | |
text: { | |
position: 'absolute', | |
left: 0, | |
top: 0, | |
right: 0, | |
bottom: 0 | |
} | |
}, | |
addPlaceholder: function(o) { | |
return function() { | |
var $self = $(this), | |
$wrap = $self.parent(), | |
value = $self.attr('placeholder'); | |
$('<span />') | |
.css(o.styles.text) | |
.addClass('ie-placeholder') | |
.text(value) | |
.appendTo($wrap) | |
.on('click', function() { | |
$(this).prev().focus(); | |
}) | |
; | |
} | |
}, | |
keyup: function() { | |
var $placeholder = $(this).next(); | |
if ($(this).val().length) { | |
$placeholder.hide(); | |
} else { | |
$placeholder.show(); | |
} | |
} | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment