See if ContentEditable div has focus

后端 未结 4 2023
独厮守ぢ
独厮守ぢ 2021-02-14 07:25

I\'m attempting to check whether or not a contenteditable div has focus, but I\'m having some trouble. Here\'s my code so far:

if ($(\"#journal-content:focus\")         


        
4条回答
  •  北海茫月
    2021-02-14 07:43

    Try:

    if ($("#journal-content").is(":focus")) {
        alert("Has Focus");
    } else {
        alert("Doesn't Have Focus");
    }
    

    Or:

    window.contenteditable_focused = false;
    
    $("#journal-content").focus(function() {
        //alert("Has Focus");
        contenteditable_focused = true;
    });
    $("#journal-content").blur(function() {
        //alert("Doesn't Have Focus");        
        contenteditable_focused = false;
    });
    

    Check for contenteditable_focused before executing your script.

    Or:

    if ($( document.activeElement ).is("#journal-content")) {
        alert("Has Focus");
    } else {
        alert("Doesn't Have Focus");
    }
    

提交回复
热议问题