js如何添加css样式

文章2022-04-06153 人已阅来源:网络

js添加css样式的方法:1、通过调用元素的css方法来添加样式;2、通过addClass来添加css样式,语句如“$("#txtName").addClass("aa");”。

js如何添加css样式

本文操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

js如何添加css样式?

添加css样式方法汇总

由于jquery支持css3,所有能很好的兼容很多浏览器,

所以通过jquery来使用css样式比较好。

为定义好的css样式可以调用元素的css方法添加样式

$("span").css("css属性名","属性值")
如:
$("span").css("color","red") 将标签为span的字体都设为红色的

如果是定义好的css样式,可以通过addClass来添加,比如

  <style type="text/css">
       .aa {border:1px solid red;}
  </style>
  <input id="txtName" type="text" value="请输入你的姓名"/>
  
  <script>
  $("#txtName").addClass("aa");
  </script>

下面列举下对css样式操作的方法:

1、.css("样式"):获得样式值,比如$("input").css("color")  获得input中字体的颜色
2、.css("样式","value"):为样式赋值,如$("input").css("color","red");
3、.addClass("样式类1,样式类2,样式类3"):可以添加多个定义好的样式类
4、.hasClass("样式类类"):判断是否存在该样式
5、.toggleClass("样式类"):如果存在(不存在)就切换(删除)样式
6、.toggleClass("样式类",swith):如果swith为false,则删除样式,如果swith为true,则切换成该类
7、.removeClass("样式类"):移除样式类
8、.css({样式名:"value",样式名:"value",样式名:"value"}):可以多次添加样式

判断display隐藏显示

   // 判断是否为隐藏(css)样式
    function isHide(obj) {
    var ret = obj.style.display === "none" ||
    obj.style.display === "" ||
    (obj.currentStyle && obj.currentStyle === "none") ||
    (window.getComputedStyle && window.getComputedStyle(obj, null).display === "none")
   
    return ret;
    }

推荐:《javascript高级教程》

以上就是js如何添加css样式的详细内容!