我有一个在线表单:
http://yoursdproperty.com/index.php?option=com_chronocontact&Itemid=56
我想在那里添加一些淡灰色的文本,让用户点击后它会消失。就像在 StackOverflow 表单中,问题标题中说“你的编程问题是什么,请详细说明?”
最简单的实现方法是什么?
我有一个在线表单:
http://yoursdproperty.com/index.php?option=com_chronocontact&Itemid=56
我想在那里添加一些淡灰色的文本,让用户点击后它会消失。就像在 StackOverflow 表单中,问题标题中说“你的编程问题是什么,请详细说明?”
最简单的实现方法是什么?
placeholder
属性非常轻松地实现 - 不确定目前有多广泛的支持。// Reference our element
var txtContent = document.getElementById("content");
// Set our default text
var defaultText = "Please enter a value.";
// Set default state of input
txtContent.value = defaultText;
txtContent.style.color = "#CCC";
// Apply onfocus logic
txtContent.onfocus = function() {
// If the current value is our default value
if (this.value == defaultText) {
// clear it and set the text color to black
this.value = "";
this.style.color = "#000";
}
}
// Apply onblur logic
txtContent.onblur = function() {
// If the current value is empty
if (this.value == "") {
// set it to our default value and lighten the color
this.value = defaultText;
this.style.color = "#CCC";
}
}