admin管理员组文章数量:1406924
So I have this textarea that expands on focus(), and returns to its original position on blur().
The issue I'm having is to stop the blur function propagation (keep the textarea focused) when a button is clicked.
$("#mytextarea").focus(function(){
$(this).animate({"height": "50px",}, "fast" );
});
$("#mytextarea").blur(function(e){
if (e.target.id === 'button'){
return false;
e.stopPropagation();
e.cancelBubble = true;
}
else{
$('#mytextarea').animate({"height": "20px",}, "fast" );
}
});
The solution I came up with is to replace:
$("#mytextarea").blur(function(e)
with
$(document).click(function(e)
But honestly I don't want to use document.click, my page is already heavy on js and using this method makes it slow. Here's a Fiddle
So I have this textarea that expands on focus(), and returns to its original position on blur().
The issue I'm having is to stop the blur function propagation (keep the textarea focused) when a button is clicked.
$("#mytextarea").focus(function(){
$(this).animate({"height": "50px",}, "fast" );
});
$("#mytextarea").blur(function(e){
if (e.target.id === 'button'){
return false;
e.stopPropagation();
e.cancelBubble = true;
}
else{
$('#mytextarea').animate({"height": "20px",}, "fast" );
}
});
The solution I came up with is to replace:
$("#mytextarea").blur(function(e)
with
$(document).click(function(e)
But honestly I don't want to use document.click, my page is already heavy on js and using this method makes it slow. Here's a Fiddle
Share Improve this question edited Jul 22, 2013 at 22:56 Omar 31.7k9 gold badges72 silver badges116 bronze badges asked Jul 22, 2013 at 22:19 AaronAaron 1811 gold badge4 silver badges12 bronze badges 3-
Three things. Code after a
return
statement doesn't execute. And you don't need to manually doe.cancelBubble = true;
; jQuery normalizes the prevention of bubbling - just calle.stopPropagation()
and you should be good to go. Usingreturn false;
in an event handler effectively does the same as manually callinge.preventDefault()
ande.stopPropagation()
...so you don't need what you have - choose what to use. – Ian Commented Jul 22, 2013 at 22:27 - @Ian Well, I just tried it using just .stopPropagation(), but the result was the same, thanks tho – Aaron Commented Jul 22, 2013 at 22:34
- 1 It's a little late for me, but I think this might be of relevance: Delegating the focus and blur events (at Quirksmode). – David Thomas Commented Jul 22, 2013 at 23:07
4 Answers
Reset to default 3So after a couple of hours I got it working, is not pretty, but after many tests appears to be ok.
var mousedownHappened = false;
$("#mytextarea").focus(function(){
$(this).animate({"height": "50px",}, "fast" );
});
$('#mytextarea').blur(function() {
if(mousedownHappened) // cancel the blur event
{
mousedownHappened = false;
}
else // blur event is okay
{
$("#mytextarea").animate({"height": "20px",}, "fast" );
}
});
$('#button').mousedown(function() {
mousedownHappened = true;
});
Here is a working Demo credit goes to @Alex b in this question: how to prevent blur() running when clicking a link in jQuery?
$("#mytextarea").focus(function(){
$(this).animate({"height": "50px",}, "fast" );
});
$("#mytextarea").blur(function(e){
if (e.target.id === 'button'){
e.cancelBubble = true;
return false; // return needs to be last
}
// don't use else when you're returning because it's not needed
$('#mytextarea').animate({"height": "20px",}, "fast" );
});
From the selected answer I have tried it to use but I have found a problem. That is If the textarea is expanded and you click the button more than two times then animation does not work If I did not focus/blur two times. So I have got another solution.
Stop Propagation is most pretty solution in this case
See JSFiddle
$(document).ready(function () {
$("#mytextarea").focus(function (e) {
$(this).animate({
"height": "50px",
}, "fast");
});
$('#button').click(function (e) {
e.stopPropagation();
});
$(document).click(function (e) {
$("#mytextarea").animate({
"height": "20px",
}, "fast");
});
});
Instead of using the "blur" event, you could use the "focusout" event. It is similar to blur but bubbles up through the DOM-tree. See this stackoverflow answer.
本文标签: javascriptBlur function event stop propagationStack Overflow
版权声明:本文标题:javascript - Blur function event stop propagation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744309805a2599973.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论