目前 JavaScript 中的 If else 函数存在问题。 if 语句有效,但 else 语句无效

Currently having an issue with my If else function in JavaScript. The if statement works but the else statement does not

您好,这是一个非常基本的问题,我目前无法解决。仅使用 JavaScript 我正在尝试创建一个类似按钮,将其文本内容从空心更改为满心,同时还具有递增和递减功能。我正在使用 unicode 作为文本内容。

我的问题是 if 语句似乎工作得很好,因为单击它确实从空心变为满心,同时也在增加,但 else 语句似乎不是。它似乎只是停留在完整的心脏中,并且只会在点击时不断增加。

我也尝试过使用 else if,但它似乎也不起作用。

如有任何建议,我们将不胜感激。

let likes = Math.floor(Math.random() * 100)+1;
const likesDiv = document.createElement("span");
const likesButton = document.createElement("button");
likesDiv.appendChild(likesButton);

const emptyHeart = '\u2661';
const fullHeart = '\u2665';
likesButton.textContent = emptyHeart;
likesDiv.innerText = `${likes}`;

likesButton.addEventListener("click", toggle)
    
    function toggle() {
      if (likesButton.textContent = emptyHeart) {
        likesButton.textContent = fullHeart
        likes++;
        likesDiv.innerText = `${likes} `;
      }
      else{
        likesButton.textContent = emptyHeart;
        likes--;
        likesDiv.innerText = `${likes} `;
      }
    };

比较是用双等号==完成的。单个等号将值赋给变量,returns 赋值。 JavaScript 会将其自动转换为布尔值,从而导致所有 truthy values.

进入 if 块