常用原生js

近期原生js用法
  • js获取元素
document.getElementById('sidebar') //获取id元素
document.getElementsByClassName('sidebar'//获取类名元素
document.querySelector("#contatiner") //获取id元素
document.querySelector(".contatiner") //获取class元素
document.querySelectorAll(".con") //获取元素集合
  • 获取元素的宽高等
    • 如果在css中未设置的属性,使用a.style.* 的方式是无法获取的
    • clientHeight (内容高度+padding * 2)
    • offsetHeight (内容高度 + 内边距*2 +边框*2)
var a = document.getElementById('a');
a.clientHeight 
a.clientWidth
a.offsetHeight
a.offsetWidth

猜你喜欢

转载自blog.csdn.net/weixin_42018790/article/details/80747630
今日推荐