下面是我的文本框的占位符在Internet Explorer中无法工作。有没有办法在Internet Explorer中显示文本框的占位符?
<asp:TextBox id="email" runat="server" width="300" placeholder="您的电子邮件" />
是否可以通过使用JavaScript来简单地解决?我不想使用jQuery使它变得复杂。
下面是我的文本框的占位符在Internet Explorer中无法工作。有没有办法在Internet Explorer中显示文本框的占位符?
<asp:TextBox id="email" runat="server" width="300" placeholder="您的电子邮件" />
是否可以通过使用JavaScript来简单地解决?我不想使用jQuery使它变得复杂。
您可以使用纯JavaScript来模仿此操作:
var _debug = false;
var _placeholderSupport = function() {
var t = document.createElement("input");
t.type = "text";
return (typeof t.placeholder !== "undefined");
}();
window.onload = function() {
var arrInputs = document.getElementsByTagName("input");
var arrTextareas = document.getElementsByTagName("textarea");
var combinedArray = [];
for (var i = 0; i < arrInputs.length; i++)
combinedArray.push(arrInputs[i]);
for (var i = 0; i < arrTextareas.length; i++)
combinedArray.push(arrTextareas[i]);
for (var i = 0; i < combinedArray.length; i++) {
var curInput = combinedArray[i];
if (!curInput.type || curInput.type == "" || curInput.type == "text" || curInput.type == "textarea")
HandlePlaceholder(curInput);
else if (curInput.type == "password")
ReplaceWithText(curInput);
}
if (!_placeholderSupport) {
for (var i = 0; i < document.forms.length; i++) {
var oForm = document.forms[i];
if (oForm.attachEvent) {
oForm.attachEvent("onsubmit", function() {
PlaceholderFormSubmit(oForm);
});
}
else if (oForm.addEventListener)
oForm.addEventListener("submit", function() {
PlaceholderFormSubmit(oForm);
}, false);
}
}
};
function PlaceholderFormSubmit(oForm) {
for (var i = 0; i < oForm.elements.length; i++) {
var curElement = oForm.elements[i];
HandlePlaceholderItemSubmit(curElement);
}
}
function HandlePlaceholderItemSubmit(element) {
if (element.name) {
var curPlaceholder = element.getAttribute("placeholder");
if (curPlaceholder && curPlaceholder.length > 0 && element.value === curPlaceholder) {
element.value = "";
window.setTimeout(function() {
element.value = curPlaceholder;
}, 100);
}
}
}
function ReplaceWithText(oPasswordTextbox) {
if (_placeholderSupport)
return;
var oTextbox = document.createElement("input");
oTextbox.type = "text";
oTextbox.id = oPasswordTextbox.id;
oTextbox.name = oPasswordTextbox.name;
//oTextbox.style = oPasswordTextbox.style;
oTextbox.className = oPasswordTextbox.className;
for (var i = 0; i < oPasswordTextbox.attributes.length; i++) {
var curName = oPasswordTextbox.attributes.item(i).nodeName;
var curValue = oPasswordTextbox.attributes.item(i).nodeValue;
if (curName !== "type" && curName !== "name") {
oTextbox.setAttribute(curName, curValue);
}
}
oTextbox.originalTextbox = oPasswordTextbox;
oPasswordTextbox.parentNode.replaceChild(oTextbox, oPasswordTextbox);
HandlePlaceholder(oTextbox);
if (!_placeholderSupport) {
oPasswordTextbox.onblur = function() {
if (this.dummyTextbox && this.value.length === 0) {
this.parentNode.replaceChild(this.dummyTextbox, this);
}
};
}
}
function HandlePlaceholder(oTextbox) {
if (!_placeholderSupport) {
var curPlaceholder = oTextbox.getAttribute("placeholder");
if (curPlaceholder && curPlaceholder.length > 0) {
Debug("Placeholder found for input box '" + oTextbox.name + "': " + curPlaceholder);
oTextbox.value = curPlaceholder;
oTextbox.setAttribute("old_color", oTextbox.style.color);
oTextbox.style.color = "#c0c0c0";
oTextbox.onfocus = function() {
var _this = this;
if (this.originalTextbox) {
_this = this.originalTextbox;
_this.dummyTextbox = this;
this.parentNode.replaceChild(this.originalTextbox, this);
_this.focus();
}
Debug("input box '" + _this.name + "' focus");
_this.style.color = _this.getAttribute("old_color");
if (_this.value === curPlaceholder)
_this.value = "";
};
oTextbox.onblur = function() {
var _this = this;
Debug("input box '" + _this.name + "' blur");
if (_this.value === "") {
_this.style.color = "#c0c0c0";
_this.value = curPlaceholder;
}
};
}
else {
Debug("input box '" + oTextbox.name + "' does not have placeholder attribute");
}
}
else {
Debug("browser has native support for placeholder");
}
}
function Debug(msg) {
if (typeof _debug !== "undefined" && _debug) {
var oConsole = document.getElementById("Console");
if (!oConsole) {
oConsole = document.createElement("div");
oConsole.id = "Console";
document.body.appendChild(oConsole);
}
oConsole.innerHTML += msg + "<br />";
}
}
如果浏览器已经支持placeholder
属性,则此操作不会产生任何效果;如果不支持(例如浏览器为IE),则将添加非常相似的功能 - 默认显示的文本在焦点消失并且用户未输入任何内容时重新出现。
错误修复
2012年11月6日:之前的代码无法检测到浏览器没有对占位符提供原生支持。使用在此其他帖子中找到的代码进行了修复。受影响的浏览器:IE7和IE8,可能还有其他浏览器。还添加了调试支持以帮助调试未来的问题。
2013年1月30日:
同时添加了对密码输入的支持。由于IE8及以下版本不允许动态更改输入类型,因此只要没有输入密码,该代码就会将密码输入替换为文本输入,从而可以看到占位符文本。
修正了提交与输入相关联的表单时导致发送占位符值的错误。为此,将代码附加到表单提交事件上,并在需要时清除该值。
2014年1月24日:添加对<textarea>
元素的支持。
我写了这段简单的代码,在测试时它运行得很好:
placeholder=function(){
$('input, textarea').each(function(){
var holder=$(this).attr('placeholder');
if(typeof(holder) !=='undefined'){
$(this).val(holder);
$(this).bind('click',function(){
$(this).val('');
}).blur(function(){
$(this).val(holder);
});
}
});
};
$(document).ready(placeholder);
if( $.browser.msie && $.browser.version <= '9.0' )
。非常感谢! - user621639 function addPlaceHolder(input) {//using modernizr add placeholder text for non html5 users
if (!Modernizr.input.placeholder) {
input.focus(function () {
if (input.val() == input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
}).blur(function () {
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder');
input.val(input.attr('placeholder'));
}
}).blur();
$(input).parents('form').submit(function () {
$(this).find(input).each(function () {
if (input.val() == input.attr('placeholder')) {
input.val('');
}
})
});
}
}
addPlaceHolder($('#Myinput'));
对我来说似乎很好用!
占位符是HTML5的一个特性。为了解决这个问题,我检测MSIE并执行以下操作:
if ( $.browser.msie ) {
$("#textarea-id").val('placeholder');
$("#textarea-id").focus(function(){
this.select();
});
}
这是jQuery,但不是那么复杂...
刚刚从上面获取了代码并将其更加通用化
<script type="text/javascript">
function addPlaceHolder(input) {
if (!Modernizr.input.placeholder) {
input.focus(function () {
if ($(this).val() == $(this).attr('placeholder')) {
$(this).val('');
$(this).removeClass('placeholder');
}
}).blur(function () {
if ($(this).val() == '' || $(this).val() == $(this).attr('placeholder')) {
$(this).addClass('placeholder');
$(this).val($(this).attr('placeholder'));
}
}).blur();
$(input).parents('form').submit(function () {
$(this).find(input).each(function () {
if ($(this).val() == $(this).attr('placeholder')) {
$(this).val('');
}
})
});
}
}
addPlaceHolder($(':text'));
addPlaceHolder($('textarea'));
</script>