See if ContentEditable div has focus

后端 未结 4 2035
独厮守ぢ
独厮守ぢ 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:46

    For pure JavaScript when you have multiple contenteditable elements:

    Check document.activeElement.id or document.activeElement.isContentEditable.

    Example:

    function isFocused() {
      if (document.activeElement.id === "journal-content") {
        alert("Focused!");
      } else {
        alert("Not focused :(");
      }
    }
    #journal-content {
      background-color: #eee;
    }
    #not-journal-content {
      background-color: #ccc;
    }
    Journal Content
    Not Journal Content

提交回复
热议问题