如何使用JavaScript设置DOM元素的样式 | 您所在的位置:网站首页 › jq动态添加样式 › 如何使用JavaScript设置DOM元素的样式 |
You might have the need to dynamically apply CSS properties to DOM elements. 您可能需要将CSS属性动态地应用于DOM元素。 What are the APIs browser expose to do that? 浏览器公开了哪些API? First, one of the cleanest ways is to add or remove classes from an element, and use classes styling in your CSS. 首先,最干净的方法之一是在元素中添加或删除类,并在CSS中使用类样式。 const element = document.querySelector('#my-element')You can use the classList property of an element and its add() and remove() methods: 您可以使用元素的classList属性及其add()和remove()方法: element.classList.add('myclass') element.classList.remove('myclass')You can also directly change each CSS prop |
CopyRight 2018-2019 实验室设备网 版权所有 |