简体   繁体   中英

jquery why isn't this working?

$(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';
        }
    });
});

I have this bit of script stuck on a php file. However it is not functioning. The textarea's default value is Post a comment. This script should change the textarea's value to null on click, and then on blur change it back to post a comment.

As zerkms said, this.'Post a comment' does not make sense.

It will give you...

Uncaught SyntaxError: Unexpected string

You probably just mean the string 'Post a comment' .

Also, this is the native DOM element, you need to wrap it with $() to make it a jQuery object.

The followig code should work:

     $(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';
                 }
             }
         );
    });

You could do it much nicer using by adding placeholder="Post a comment" to your <textarea> . While this is not supported by some older browsers it's working fine in modern browsers and does not need any scripts.

Demo: http://jsfiddle.net/ThiefMaster/vVxrp/

this code i think should work(if you're using jQuery):

$(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');
    }
    }
    );
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM