gpt4 book ai didi

javascript - 使用 javascript 显示/隐藏 html 元素

转载 作者:行者123 更新时间:2023-12-02 18:34:25 24 4
gpt4 key购买 nike

以下选项中哪个更好或用作显示/隐藏 html 元素的标准方法

  1. 更改 element.style.display
  2. 添加/删除名为 hide {display: none} 的单独类
  3. 任何其他标准方式

PS:这个JavaScript hide/show element问题使用提到的第一个选项(将样式更改为阻止以显示可能不需要的选项)。我想知道大多数网站是否使用此方法或添加/删除单独的类或任何其他方式

  • 下面答案中的第三种方式 https://stackoverflow.com/a/68983509/14478972
  • 最佳答案

    我更喜欢使用 DOMTokenList.toggle() 切换类(class):

    The toggle() method of the DOMTokenList interface removes a given token from the list and returns false. If token doesn't exist it's added and the function returns true.

    关于javascript - 使用 javascript 显示/隐藏 html 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68983182/

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