JQuery操作class属性
函数 | 函数说明 |
---|---|
addClass(“属性值”) | 给JQ元素对象添加一个class属性 |
removeClass() | 移除class属性 |
隔行换色代码示例
<style>
.blue{
background-color: lightblue;
}
</style>
<script type="text/javascript" src="../js/jquery-3.3.1.js" ></script>
<script>
$(function(){
//1.获取除了第一行的所有<tr>标签
var arr = $("tr:not(:first)");
//2.偶数行(索引为奇数的行)添加class属性,值为blue
for(var i=0;i<arr.length;i++){
if(i % 2 != 0 ){
//这里arr[i]会转成JS对象,需要重新转成JQ对象,去调用JQ的addClass()方法
$(arr[i]).addClass("blue");
}
}
});
</script>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" width="80%">
<tr>
<th></th>
<th>序号</th>
<th>商品名称</th>
<th>商品描述</th>
<th>操作</th>
<