鱼C论坛

 找回密码
 立即注册
查看: 3521|回复: 1

[已解决]js获取css样式的问题(新手)

[复制链接]
发表于 2021-6-21 20:35:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. <!DOCTYPE html>
  2. <html>
  3.         <head>
  4.                 <meta charset="utf-8">
  5.                 <title></title>
  6.                 <style type="text/css">
  7.                         .box1{
  8.                                 width: 100px;
  9.                                 height: 100px;
  10.                                 background-color: orangered;
  11.                         }
  12.                 </style>
  13.         </head>
  14.         <body>
  15.                 <div class="box1"></div>
  16.                 <button type="button" id="color" onclick="gaiColor">点我更改颜色</button>
  17.                 <button type="button" id="color" onclick="getColor">点我获取颜色</button>
  18.         </body>
  19.         <script type="text/javascript">
  20.                 var a = document.getElementsByClassName("box1")[0];
  21.                 var getColor = function(){
  22.                         alert(a.currentstyle.backgroundColor);
  23.                 }
  24.                 var gaiColor = function(){
  25.                         a.style.backgroundColor = "red";
  26.                 }
  27.         </script>
  28. </html>
复制代码




这两个获取样式和更改样式的按钮无效
最佳答案
2021-6-21 23:18:50
a.currentstyle未定义 getColor执行会报错
button.onclick传字符串是eval执行  执行函数名需要加括号
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-21 23:18:50 | 显示全部楼层    本楼为最佳答案   
a.currentstyle未定义 getColor执行会报错
button.onclick传字符串是eval执行  执行函数名需要加括号
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 15:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表