12样式操作
获取内联样式
<p id="p1" style="color: red;">666</p>- 内联样式即写在标签里的样式(返回文本类型)
var pElement = document.getElementById('p1').getAttribute('style')
console.log(pElement)- 通过DOM对象获取。(返回对象)
var pElement = document.getElementById('p1')
console.log(pElement.style)对象的方法如下:
cssText声明块的文本内容
length属性数量
item()属性名
getPropertyValue()属性值
获取外联样式
var stylesheetList = document.styleSheets
console.log(stylesheetList)其中CSSRule对象包含一组css规则
可以修改href文件实现更换全局样式。
获取当前有效样式
var i = document.getElementsByTagName('input')[0]
console.log(window.getComputedStyle(i))返回结果为CSSStyleDeclaration
IE 8 之前版本不支持
getComputedStyle属性。可以使用currentStyle属性代替。
设置内联样式
直接通过
currentStyle<div id="box" class="box">test</div> <script> var box = document.getElementById('box'); box.style = 'color:red;' </script>这种方式定义CSS样式,是字符串类型。并不适用于定义过多的CSS样式属性。
通过调用
element.setAttribute()方式调用<div id="box" class="box">test</div> <div id="box1" class="box" style="color: red;">移除元素测试</div> <script> var box = document.getElementById('box'); var box1 = document.getElementById('box1'); // 添加属性 box.setAttribute('style', 'color:red') // 删除属性 box1.removeAttribute('style') </script>通过
style的方法(setPropertyremoveProperty)<div id="box" class="box">test</div> <div id="box1" class="box" style="color: red;">移除元素测试</div> <script> var box = document.getElementById('box'); var box1 = document.getElementById('box1'); // 添加属性 box.style.setProperty("color", "red") // 移除属性 box1.style.removeProperty('color') </script>
设置class样式
Element对象的className属性
<style> .red { color: red; } </style> <body> <p id="test" class="test">这是一个test</p> <script> var test = document.getElementById('test') console.log(test.className) test.className = 'red' </script> </body>Elememnt对象的classList属性
<style> .red { color: red; } </style> <body> <p id="test1" class="test">this is a test1</p> <p id="test2" class="red test">this is a test2</p> <p id="test3-1" class="test">this is a test3-1</p> <p id="test3-2" class="test red">this is a test3-2</p> <p id="test4" class="test">this is a test4</p> <script> // 获取class为test的全部标签 var tests = document.getElementsByClassName("test") // 为第一个p标签添加class名 red tests[0].classList.add('red') // 删除第二个标签class内的red tests[1].classList.remove('red') // 如果存在就删除,不存在就添加 tests[2].classList.toggle('red') tests[3].classList.toggle('red') // 检查元素class是否含有red console.log(tests[4].classList.contains('red')) // 输出索引对应的类型 console.log(tests[3].classList.item(0)) </script> </body>方法名 描述 add()添加指定的类值。如果这些类已经存在于元素的属性中,那么它们将被忽略 remove()删除指定的类值 item()按集合中的索引返回类值 toggle()切换指定的类值 contains()检查元素的类属性中是否存在指定的类值
Element对象的样式属性
clientWidthwidth+padding-滚动条的宽度(如果存在)clientHeightheight+padding-滚动条的高度(如果存在的话)scrollWidth返回元素内容区的宽度和元素本身宽度中更大的值
scrollHeight表示元素内容的高度。包含
overflow样式属性导致不可见的内容区scrollLeft表示滚动条到元素左边的距离。默认值0
scrollTop表示滚动条到元素顶部的距离。默认值0
判断元素是否滚动到底部
element.scrollHeight - element.scrollTop === element.clientHeight获取指定元素的定位父元素
element.offsetParent作为返回值的 parentObj表示一个指向最近的包含该元素的定位元素
注意:如果祖先元素中没有开始定位,则 offsetParent为body元素。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 小康博客!
评论
TwikooDisqusjs















