1 Star 0 Fork 0

恒星观测员340号 / 待办事项

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

学习笔记

HTMLCollection to Array in Javascript

var arr
var collection = document.getElementsByTagName('div')

// long version
arr = Array.prototype.slice.call(collection)
// short version
arr = [].slice.call(collection)
// ES6 version
arr = [...collection]

限制鼠标事件对覆盖元素层进行穿透

<button class="complete-btn">
    <i class="fas fa-check"></i>
</button>
<button class="trash-btn">
    <i class="fas fa-trash"></i>
</button>
.fa-trash,
.fa-check {
    pointer-events: none;
}

这样,点击按钮内部图标的效果就会失效


How to add, remove and toggle CSS classes in JavaScript

classList 的使用

<div class="hot spicy pizza">🍕</div>
// grab element reference
const pizza = document.querySelector('.pizza')

// get all existing classes
console.log(pizza.classList)
// ["hot", "spicy", "pizza", value: "hot spicy pizza"]

// get first class name
console.log(pizza.classList[0]) // hot

// get total number of classes
console.log(pizza.classList.length) // 3

add() 方法

The add() method adds one or more classes to the HTML element:

pizza.classList.add('sauce', 'cheese', 'tomato')

Now, the element looks like below:

<div class="hot spicy pizza sauce cheese tomato">🍕</div>

remove() 方法

The remove() method is used to remove one or more classes from the element:

pizza.classList.remove('sauce', 'potato')

If you try to remove a class that doesn't exist, as we did in the above example, there won't be any error. JavaScript will simply ignore it.

contains() 方法

The contains() method returns true if the element contains the given class, otherwise false:

console.log(pizza.classList.contains('cheese')) // true
console.log(pizza.classList.contains('yogurt')) // false

toggle() 方法

toggle() is an interesting method. It removes the class if it already exists, otherwise, it adds it to the collection.

Without this method, you have to manually check if the class exists using contains() before toggling it on or off:

// manual toggle
if (pizza.classList.contains('olive')) {
    pizza.classList.remove('olive')
} else {
    pizza.classList.add('olive')
}

With the toggle() method, you simply pass the name of the class which you want to toggle:

pizza.classList.toggle('olive')

The toggle() method returns true if the class was added, and false if it was removed:

const status = pizza.classList.toggle('olive')

console.log(status) // true --> class was added

You can also pass a second boolean parameter to the toggle() method to indicate whether to add the class or remove it. This will turn toggle() into one way-only operation. If the second argument evaluates to false, then the class will only be removed, but not added. If it evaluates to true, then the class will only be added, but not removed.

Here is an example:

const status = pizza.classList.toggle('hot', false)

console.log(status) // false --> class was removed
MIT License Copyright (c) 2020 恒星观测员340号 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/faiz-lab/to-do-list.git
git@gitee.com:faiz-lab/to-do-list.git
faiz-lab
to-do-list
待办事项
master

搜索帮助