2011-04-24 13 views
0
$(document).ready(function() 
{ 
    $('#comment textarea').click(function() 
    { 
     if (this.value == this.'Post a comment') 
     { 
      this.value = ''; 
     } 
    }); 

    $('#comment textarea').blur(function() 
    { 
     if (this.value == '') 
     { 
      this.value = this.'Post a comment'; 
     } 
    }); 
}); 

나는이 스크립트 파일을 PHP 파일에 붙여두고 있습니다. 그러나 작동하지 않습니다. 텍스트 영역의 기본값은 주석 게시입니다. 이 스크립트는 텍스트 영역의 값을 클릭시 null로 변경 한 다음 blur에서 주석을 게시하기 위해 다시 변경해야합니다.jquery 왜이 기능이 작동하지 않습니까?

+5

는'this.value ==이 comment''을 this.'Post은 ---이 무엇인가!? – zerkms

답변

1

:

 $(document).ready(function() { 
     $('#comment textarea').click(
      function() { 
       if (this.value == 'Post a comment') { 
        this.value = ''; 
       } 
      } 
     ); 
     $('#comment textarea').blur(
      function() { 
       if (this.value == '') { 
        this.value = 'Post a comment'; 
       } 
      } 
     ); 
    }); 
2

zerkms으로 말하면, this.'Post a comment'은 의미가 없습니다.

그것은 당신에게 줄 것이다

...

Uncaught SyntaxError: Unexpected string

당신은 아마 문자열 'Post a comment'을 의미한다.

또한 this이 기본 DOM 요소이므로이 요소를 $()으로 묶어 jQuery 객체로 만들어야합니다.

0

(당신이 jQuery를 사용하는 경우) 내가 일을해야한다고 생각이 코드 : followig 코드가 작동합니다

$(document).ready(function() { 
    $('#comment textarea').click(
    function() { 
    if ($(this).val() == 'Post a comment') { 
    $(this).val(''); 
    } 
    } 
    ); 
    $('#comment textarea').blur(
    function() { 
    if ($(this).val() == '') { 
    $(this).val('Post a comment'); 
    } 
    } 
    ); 
    }); 
1

당신은 사용이 훨씬 좋네요 할 수에 placeholder="Post a comment"을 추가하여 귀하의 <textarea>. 일부 구형 브라우저에서는 지원하지 않지만 최신 브라우저에서는 잘 작동하며 스크립트가 필요하지 않습니다.

데모 : http://jsfiddle.net/ThiefMaster/vVxrp/

+0

+1은'placeholder' 속성 언급에 +1합니다. – alex

관련 문제