javascript - MouseoverMouseOut jquery - Stack Overflow

I realize how simple this question should be to answer but I am in a medication fog and the answer is e

I realize how simple this question should be to answer but I am in a medication fog and the answer is escaping me.

I would like to make this into a simple function to display specific text if the value of the text box is empty upon mouseout and to empty out the text value upon mouseover.

What I have right now that works but is very ugly:

$(".disappearOnClick").live('mouseover',function() {    
            if($(this).val() === 'BFA Offset') {
                $(this).val('')
            }
        });

    $(".disappearOnClick").live('mouseout',function() {
            if($(this).val() === '') {
                $(this).val('BFA Offset')
            }
        });

I realize how simple this question should be to answer but I am in a medication fog and the answer is escaping me.

I would like to make this into a simple function to display specific text if the value of the text box is empty upon mouseout and to empty out the text value upon mouseover.

What I have right now that works but is very ugly:

$(".disappearOnClick").live('mouseover',function() {    
            if($(this).val() === 'BFA Offset') {
                $(this).val('')
            }
        });

    $(".disappearOnClick").live('mouseout',function() {
            if($(this).val() === '') {
                $(this).val('BFA Offset')
            }
        });
Share Improve this question asked Nov 7, 2011 at 14:23 Michael BWMichael BW 1,0703 gold badges13 silver badges26 bronze badges 7
  • you are missing semicolon javascript – Ali Nouman Commented Nov 7, 2011 at 14:25
  • the semicolon is not required unless this is all on 1 line. – kamui Commented Nov 7, 2011 at 14:33
  • You can use hover instead of mouseover and mouseout. Syntax: $(element).hover(function_on_hover, function_on_mouseout); – Rob W Commented Nov 7, 2011 at 14:33
  • @RobW but what about the live() function ? can you bine live() and hover() ? – Manse Commented Nov 7, 2011 at 14:36
  • 2 @kamui "the semicolon is not required unless this is all on 1 line." dear god save us all. – jbabey Commented Nov 7, 2011 at 14:40
 |  Show 2 more ments

5 Answers 5

Reset to default 4

You can bind to multiple events using the live() method - so you could use something like this ->

$('.disappearOnClick').live('mouseover mouseout', function(event) {
  if (event.type == 'mouseover') {
     if($(this).val() === 'BFA Offset') {
            $(this).val('');
        }
  } else {
    if($(this).val() === '') {
            $(this).val('BFA Offset');
        }
  }
});
$(".disappearOnClick").mouseover(function(){...});

and

$(".disappearOnClick").mouseout(function(){...});

Would work just as well.

You should use hover instead:

$(".disappearOnClick").hover(
    function(){
        //mouseover
    },
    function(){
        //mouseout
    }
);

You could try something like this (you could of course change the focus/blur events to mouse-events):

http://jsfiddle/BD7JA/2/

// <input value="BFA Offset" data-placeholder="BFA Offset" class="is-placeholder" />

$('[data-placeholder]').on({
  focus: function (evt) {
    if ($(this).hasClass('is-placeholder')) {
      $(this).val('');
      $(this).removeClass('is-placeholder');
    }
  },
  blur: function (evt) {
    if ($(this).val() === '') {
      $(this).val($(this).data('placeholder'));
      $(this).addClass('is-placeholder');
    }
  }
});

Try this:

$(".disappearOnClick").mouseenter( function (this) {
    if ($('#'+this.id).val() == 'BFA Offset') 
        $('#'+this.id).val('')
}).mouseleave( function (this) {
    if ($('#'+this.id).val() == '') 
        $('#'+this.id).val('BFA Offset')
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745596301a4635136.html

相关推荐

  • javascript - MouseoverMouseOut jquery - Stack Overflow

    I realize how simple this question should be to answer but I am in a medication fog and the answer is e

    14小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信