1228 次
3 回答
1
You can escape them with \
if (this.value == '') {
this.value = 'I\'d be happy to make this introduction if possible. Contact me at your convenience.';
}
于 2013-07-18T16:53:24.627 回答
1
All you need is
window.onload=function() {
var txtArea=document.getElementById("accept-response-text");
txtArea.onfocus=function() {
if (this.value === this.defaultValue) {
this.value = '';
}
}
txtArea.onblur=function() {
if (this.value === '') {
this.value = this.defaultValue;
}
}
}
UPDATE
Here is a version with placeholder and support for browsers without placeholders
function hasPlaceHolder() {
var i = document.createElement('input');
return 'placeholder' in i;
}
window.onload=function() {
var txt_area_accept = document.getElementById("accept-response-text");
if (!hasPlaceHolder()) {
txt_area_accept.defaultValue=txt_area_accept.getAttribute("placeholder");
txt_area_accept.onfocus = function() {
if (this.value === this.defaultValue) {
this.value = '';
}
}
txt_area_accept.onblur = function() {
if (this.value === '') {
this.value = this.defaultValue;
}
}
}
}
于 2013-07-18T16:59:36.707 回答
1
The code you posted in your update is correct, except that for adding events you assign to the onfocus and onblur, not just focus and blur.
That said, you should really be using the placeholder attribute instead of what you're currently using.
于 2013-07-18T17:52:29.260 回答