textarea onclick remove text

别等时光非礼了梦想. 提交于 2019-12-30 03:43:04

问题


I know how to remove text in a simple html textbox but html textareas seem much more complicated. instead of the value attribute you put the text right between:

 <html>
<textarea> </textarea>.  
</html>

This is why im having trouble making an onFocus and onBlur event.


回答1:


<textarea name="message" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;">
Put anything for default value here
</textarea>

Live example: http://jsfiddle.net/SRYLg/




回答2:


A textarea behaves like other <input> elements (with type text or password), instead of having a value attribute, the value is between the <textarea> and </textarea> tags.

Accessing and modifying the contents of the textfield is no difference. The below code displays a textarea and an input box. The same function is used for accessing the values and modifying it. If the value equals to "example text" when entering the input, the text is cleared. If the textarea / input box is empty when leaving it, "example text" will be put in it.

<textarea id="field1">example text</textarea>
<input id="field2" value="example text">
<script>
function addEvents(id) {
    var field = document.getElementById(id);
    field.onfocus = function () {
        if (this.value == "example text") {
            this.value = "";
        } 
    };
    field.onblur = function () {
        if (this.value == "") {
            this.value = "example text";
        } 
    };
}
addEvents("field1");
addEvents("field2");
</script>
  • Live example



回答3:


Your Javascript should have:

function RemoveText(obj) 
{   obj.value = ''; } 

And Your HTML element should have:

onfocus="RemoveText(this);"



回答4:


what about calling a javascript function during the onFocus event?

function emptyText(){    
    document.getElementById(textarea).innerHTML = "";
}


来源:https://stackoverflow.com/questions/6364015/textarea-onclick-remove-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!