admin管理员组文章数量:1023773
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 ofmouseover
andmouseout
. 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 binelive()
andhover()
? – 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
5 Answers
Reset to default 4You 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')
});
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 ofmouseover
andmouseout
. 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 binelive()
andhover()
? – 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
5 Answers
Reset to default 4You 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')
});
本文标签: javascriptMouseoverMouseOut jqueryStack Overflow
版权声明:本文标题:javascript - MouseoverMouseOut jquery - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745596298a2158191.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论