简体   繁体   English

Mouseover / MouseOut jQuery

[英]Mouseover/MouseOut jquery

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

You can bind to multiple events using the live() method - so you could use something like this -> 您可以使用live()方法绑定到多个事件-这样就可以使用类似的东西->

$('.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: 您应该改用hover

$(".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.net/BD7JA/2/ http://jsfiddle.net/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')
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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