简体   繁体   English

可拖动取消触发模糊事件

[英]Draggable cancel triggering blur event

I have a draggable div which of course could be dragged around. 我有一个可拖动的div当然可以被拖走。 Inside, I have input text which has two events (focus and blur). 在里面,我输入了两个事件(焦点和模糊)的文本。 Also inside, I have another div which serve as draggable cancellation div (dragging that div prevent any drag to occur). 在内部,我有另一个div作为可拖动的取消div(拖动该div防止任何拖动发生)。

you could see it here: http://jsfiddle.net/wUDzh/4/ 你可以在这里看到它: http//jsfiddle.net/wUDzh/4/

  • Clicking the input box would trigger focus event and clicking outside main div (the red one) would trigger blur event. 单击输入框将触发焦点事件并单击外部主div(红色部分)将触发模糊事件。 This is fine. 这可以。
  • Clicking or dragging the main div would NOT trigger blur event. 单击或拖动主div不会触发模糊事件。 That's what I want. 这就是我想要的。
  • The problem is, clicking the cancellation div (the blue one) cause blur event of the input box to occur. 问题是,单击取消div(蓝色部分)会导致输入框的模糊事件发生。 Which is make sense, but that's not what I want. 这是有道理的,但那不是我想要的。

is there any way to prevent blur event to occur just like the behavior of the main div? 有没有办法防止模糊事件发生就像主div的行为?

thx 谢谢

You could do 你可以做到

$("#text").blur(function(e)
{
   // alert('a');
    if(e.target.id === 'text'){
        //this prevents other blur handlers from firing
        e.stopImmediatePropagation();
        //this prevent the default action and returns
        return false;
    }
   $(this).val("I've got blur");
});

fiddle here http://jsfiddle.net/wUDzh/6/ 在这里摆弄http://jsfiddle.net/wUDzh/6/

To prevent the input field from blurring when you click the cancellation div , simply do: 要在单击取消div时防止输入字段模糊,只需执行以下操作:

    $('#cancel').mousedown(function(e) {
        e.preventDefault();
    });

See fiddle 小提琴

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

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