-1

当用户单击文本输入控件时,我试图清除文本,如下所示,但它没有发生......下面是代码

 var generate_message = $("<textarea onclick='if (this.value == 'Enter message here...') this.value = '''  rows='5' cols='45' id='comment'>Enter message here...</textarea>");            
4

2 回答 2

1

You cannot use single quote in html and javascript. It causes confusion in browser renderer

Usually, I separate the use of single quote and double quote. I use double quote for html and single quote for javascript

See example below

<textarea onclick="if (this.value == 'Enter message here...') this.value = ''"  rows="5" cols="45" id="comment">Enter message here...</textarea>​​​​​​​​​​​​​​​​​​​​
  • Add Fiddle

See Fiddle example

  • UPDATE I just realised that you have $("...") so what i suggest you can escape the single quote by putting \

see updated fiddle here

于 2012-04-20T04:13:57.083 回答
1

我想以不引人注目的方式做到这一点

$(function(){
    $(document).on("focus","#comment",function(){
    if($(this).val()=="Enter message here...")
       $(this).val("")   
    });        
});

这是示例http://jsfiddle.net/8UjkA/11/

于 2012-04-20T04:39:41.523 回答