方法一
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return obj.getComputedStyle(obj,null)[attr]
}
方法二
var dom = docment.getElementById('div');
//获取样式
var attr = dom.style.height
//设置样式
dom.style.height = 111+'px';
方法三
dom.class 获取css样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
#aa {
width: 100px;
height: 100px;
background-color: #f00;
}
</style>
<body>
<a id="aa" x='x' href="www.baidu.com">百度</a>
<script>
let aa = document.querySelector('#aa')
console.log('aa', aa, '\n', 'aa.x', aa.x, '\n', 'aa.getAttribute(x)', aa.getAttribute('x'), 'aa.href', aa.href, '\n', 'aa.getAttribute', aa.getAttribute('href'))
aa.xx = 'xx'
aa.setAttribute('xxx', 'xxx')
console.log('aa', aa, '\n', 'aa.xx', aa.xx, '\n', 'aa.getAttribute(xx)', aa.getAttribute('xx'), '\n', 'aa.xxx', aa.xxx, '\n', 'aa.getAttribute(xxx)', aa.getAttribute('xxx'), '\n', 'aa.href', aa.href, '<br>', '\n', 'aa.getAttribute', aa.getAttribute('href'))
aa.removeAttribute('href')
aa.removeAttribute('x')
aa.removeAttribute('xx')
aa.removeAttribute('xxx')
console.log('aa', aa, '\n', 'aa.xx', aa.xx, '\n', 'aa.getAttribute(xx)', aa.getAttribute('xx'), '\n', 'aa.xxx', aa.xxx, '\n', 'aa.getAttribute(xxx)', aa.getAttribute('xxx'), '\n', 'aa.href', aa.href, '<br>', '\n', 'aa.getAttribute', aa.getAttribute('href'))
console.log(getComputedStyle(aa,null)['width'] )
window.getComputedStyle(document.body,null).background;
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
li {
height: 108px;
line-height: 108px;
}
li:nth-child(2n+1) {
background-color: rgba(0, 255, 0, 0.5);
}
</style>
</head>
<body>
<ul>
<li id="one" data-one="one">1</li>
<li id="tow" onclick="tow()" data-two="two">2</li>
<li id="three" onclick="three(this)" data-two="three">3</li>
</ul>
<script>
let one = document.getElementById('one');
one.onclick = function (e) {
console.log(e.target.id)
console.log(e.target.dataset)
}
function tow(e) {
console.log(e, '===e')
}
function three(val) {
console.log(val.id, '===id')
console.log(val.dataset, '===dataset')
}
</script>
</body>
</html>