请问js如何根据class的名字获取css属性代码?
网友回复
1、原生js这样来获取:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>js获取某元素的class里面的css属性值</title> <style> #box1 { margin: 5px; padding: 5px; height: 100px; width: 200px; } a { border: 1px solid #ccc; border-radius: 3px; padding: 3px 5px; margin: 5px 0; display: inline-block; background: #eee; color: #f60; text-decoration: none; font-size: 12px; } a:hover { color: #ff0000; background: #fff; } </style> </head> <body> <div id="box1"> box1的css.#box1{margin:5px;padding:5px;height:100px;width:200px;} </div> <a id="href1" href="BfwJavascript;"BfwOnclick="getMarginTop()">获取box1的margin-top</a><br /> <a href="BfwJavascript;"BfwOnclick="getcss('paddingTop')">获取box1的padding-top</a><br /> <a href="BfwJavascript;"BfwOnclick="getcss('height')">获取box1的height</a><br /> <script> //获取class里面的属性值 var divs = document.getElementById("box1"); function getStyle(obj, attr) { var ie = !+"\v1"; //简单判断ie6~8 if (attr == "backgroundPosition") { //IE6~8不兼容backgroundPosition写法,识别backgroundPositionX/Y if (ie) { return obj.currentStyle.backgroundPositionX + " " + obj.currentStyle.backgroundPositionY; ...
点击查看剩余70%
win10如何修改机器码?
python结合ffmpeg如何实现窗口实现视频格式转换压缩、分离音频,合并字幕及音频?
Pogocache比redis更快更好用?
新手如何使用yolo进行图像数据标注与图像识别?
threejs如何将glb三维模型减少面数和定点数粗糙一些减少文件体积大小?
在哪可以免费下载各国的武器飞机坦克航母舰艇导弹三维模型glb文件?
aistudio中build如何破解gemini的api模型调用免费使用?
python如何让红绿灯看懂两侧车流智能调节红绿灯?
python+faster_whisper如何实现实时开会录音并转成文字?
python如何调用微信本地的ocr进行图片文字提取?