那么你这里就只要设置以下两步代码即可:
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 是不是没写啊
jquery比较简单,通过移除与添加class来操作:
$(".classnameA").removeClass("classnameA").addClass("classnameB")js直接把classname属性替换即可。
var classAElements = document.getElementsByClassName("classnameA")//getElementsByClassName获取为数组,则用循环来修改
for(var a in classAElements){
classAElements[a].classname = classAElements[a].classname.replace("classnameA", "classnameB")
}