gpt4 book ai didi

javascript - !variable 在 javascript 中不起作用

转载 作者:行者123 更新时间:2023-12-03 16:41:47 24 4
gpt4 key购买 nike

我有一个带有代码的javascript:书签

javascript:document.body.contentEditable = !document.body.contentEditable; 

它应该打开和关闭页面的“编辑器”(仅用于对 friend 的恶作剧等)。但它没有达到预期的结果,当我点击书签时没有任何反应。打开 Javascript 控制台,我看到:

document.body.contentEditable
"false"
!document.body.contentEditable
false

以前,我使用了 javascript:document.body.contentEditable = true; 这使得页面可编辑,但我无法将其关闭。

最佳答案

您可能在 JavaScript 控制台中注意到,document.body.contentEditable 是一个字符串,而不是 bool 值。您可以这样做:

document.body.contentEditable = !(document.body.contentEditable == "true");

或者只是

document.body.contentEditable = document.body.contentEditable != "true";

The HTMLElement.contentEditable property is used to indicate whether or not the element is editable. This enumerated attribute can have the following values:

  • "true" indicates that the element is contenteditable.
  • "false" indicates that the element cannot be edited.
  • "inherit" indicates that the element inherits its parent's editable status.

https://developer.mozilla.org/en/docs/Web/API/HTMLElement/contentEditable

关于javascript - !variable 在 javascript 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36797689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com