javascript JavaScript和DOM第2部分:内联CSS

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript JavaScript和DOM第2部分:内联CSS相关的知识,希望对你有一定的参考价值。

/*Set INLINE CSS to an element*/
document.querySelector(".cta a").style.color = "green";
document.querySelector('.cat a').style.cssText = "padding: 1em; color: white; background-color: blue;"; // use to change entire inline style for that element.

.hasAttribute("style"); //does the element have a current inline style applied?
.getAttribute("style"); //get the current inline styles
.setAttribut("style", "color: red"); //add inline css
.removeAttribute("style"); //remove all inline styles for that element.

document.querySelector('.cta a').setAttribute('style', "padding: 2em");

// //CAVEAT
// Inline styles override whatever the style sheet is doing, which can be problematic. If you're using JavaScript to apply basic style changes, it is usually better to create a custom CSS rule targeting class selectors, and adding or removing these classes from the element instead. It's cleaner, easier to manage, and preserves the separation between content, presentation and interactivity, we talked about in the very beginning of the course.

以上是关于javascript JavaScript和DOM第2部分:内联CSS的主要内容,如果未能解决你的问题,请参考以下文章

请资深专家解答,javascript的dom问题?

javascript JavaScript和DOM

JavaScript DOM 编程艺术 笔记

JavaScript的BOM和DOM有啥区别

Python javascript操作DOM

javascript JavaScript和DOM第2部分:内联CSS