
以给 body 标签添加 class 为例
通过 jQuery
$( 'body').addClass( 'class1 class2' )$( 'body' ).removeClass( 'class1 class2' )
支持 classList 的高级浏览器(IE10+,Chrome,Firefox,Safari)
document.body.classList.add( 'class1', 'class2' )document.body.classList.remove( 'class1', 'class2' )
不支持 classList 的浏览器只能通过 className 来添加
document.body.className += ' class1 class2'其实只需要用到document.querySelector('选择器')和结合classList. add("类名")即可。那么你这里就只要设置以下两步代码即可:
document.querySelector('# room>div'). classList. add(" test")
document.querySelector('# room>div>input'). classList. add(" test2")
<!doctype html><html lang="zh-cn">
<head>
<meta http-equiv="content-type" content="text/htmlcharset=utf-8" />
<title>Document</title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<style type="text/css">
.className { background-color: #333}
</style>
</head>
<body>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<th scope="col">a</th>
<th scope="col">b</th>
<th scope="col">c </th>
<th scope="col">d </th>
</tr>
<tr><td colspan="4">e</td></tr>
<tr>
<td colspan="2">f</td>
<td colspan="2">g</td>
</tr>
<tr> <td colspan="4">h</td> </tr>
<tr>
<td>j</td>
<td>k</td>
<td>l</td>
<td>m</td>
</tr>
</table>
<script type="text/javascript">
$("td[colspan='4']").each(function(){
$(this).addClass('className')
})
//所有th
$('th').each(function(){
$(this).addClass('className')
})
</script>
</body>
</html>
//css 是不是没写啊
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)