AJa*_*arg 0 html javascript textbox
如果我提出这个问题我错了,请不要投反对票.我有一个像这样的TextBox:
<input class="login-inpt" runat="server" name="loginName" type="text" value="User Name" />
Run Code Online (Sandbox Code Playgroud)
它目前拥有默认值.我希望在单击TextBox时清除默认值并用光标替换.
对于较新的浏览器(IE> 9):
<input type="text" id="userName" placeholder="User Name" />
Run Code Online (Sandbox Code Playgroud)
对于老年人:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Placeholder support</title>
</head>
<body>
<input type="text" id="userName" placeholder="User Name" />
<script src="../js/vendor/jquery-1.10.2.min.js"></script>
<script src="../js/vendor/jquery-migrate-1.2.1.js"></script>
<script src="../js/vendor/modernizr-latest.js"></script>
<script src="../js/placeholder.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
placeholder.js(jQuery,Modernizr)
var Placeholder = (function ($, document) {
/* Static Content */
var _$document = $(document);
/* Events */
var _initPlaceholders = function () {
_$document.find('input:text').each(function () {
var $this = $(this);
$this.css({ color: '#888' }).val($this.attr('placeholder'));
});
};
var _placeholderEvent = function () {
var $input = $(this), placeholderValue = $input.attr('placeholder');
if ($input.val() === placeholderValue) {
$input.css({ color: '#000' }).val('');
return;
}
if ($input.val() === '') {
$input.css({ color: '#888' }).val(placeholderValue);
}
};
var _bindEvents = function () {
if (!Modernizr.input.placeholder) {
_initPlaceholders();
_$document.on('focus blur', 'input:text', _placeholderEvent);
}
};
var init = function () {
_bindEvents();
};
return {
init: init
};
})(jQuery, document);
Placeholder.init();
Run Code Online (Sandbox Code Playgroud)