最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Focusout only when another element not clicked - Stack Overflow

programmeradmin4浏览0评论

When the user focuses on a certain element, I display a save button. On focusout, I remove the save button.

The user can submit the input by either hitting return or click save. When they click the save button, the input loses focus and the save button is removed, thus not registering the click. Can I tell in the focusout, if the save button has been clicked?

Within my focus function I do something like this:

        $('#save_button').click(function(){
            saveEditingField(this); //save input
            $('#save_button').die("click");
        });

        $('.editing').focusout( function(e) {
            $('#isediting').attr('value','false');
            $('#edit_controls').remove()
        });

I've tried adding a delay to the remove(), but when tabbing between inputs it shows multiple save buttons (while the others are being removed).

Any ideas?

When the user focuses on a certain element, I display a save button. On focusout, I remove the save button.

The user can submit the input by either hitting return or click save. When they click the save button, the input loses focus and the save button is removed, thus not registering the click. Can I tell in the focusout, if the save button has been clicked?

Within my focus function I do something like this:

        $('#save_button').click(function(){
            saveEditingField(this); //save input
            $('#save_button').die("click");
        });

        $('.editing').focusout( function(e) {
            $('#isediting').attr('value','false');
            $('#edit_controls').remove()
        });

I've tried adding a delay to the remove(), but when tabbing between inputs it shows multiple save buttons (while the others are being removed).

Any ideas?

Share Improve this question edited Dec 15, 2022 at 22:22 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 18, 2010 at 11:35 christo16christo16 4,8436 gold badges43 silver badges53 bronze badges 2
  • would it help to use .hide() instead of .remove() ? – Shawn Chin Commented Nov 18, 2010 at 11:52
  • Even with hide it will hide the element before registering the click. – christo16 Commented Nov 18, 2010 at 12:05
Add a ment  | 

3 Answers 3

Reset to default 8

I think you only need a little timeout between focusout and removing/hiding the button.

You can check if the mouse is over the button on focusout before hiding the button.

Proof of concept here: http://jsfiddle/dnsEM/

A minor issue with this implementation is that the save button doesn't disappear if the input box looses focus while the mouse is over the button and only gets hidden on mouse out.

Creates a div containing both elements (The input text and the button) and bind the focusout event to that div

发布评论

评论列表(0)

  1. 暂无评论