在JS中如何实现让动态添加后的表格TD继承TABLE的class

在JS中如何实现让动态添加后的表格TD继承TABLE的class,第1张

无论是动态还是静态,它的样式都可以被已经写好的css定义

你只需动态添加class,

$("p:first").addClass("intro")

也可以在你动态添加表格的时候,改一下代码,直接把class也设置了,让它和原来添加前一样。

当然啊,你可以动态改变style,

$(".er").css('color',‘red’)

但这个很麻烦,不适合大量改动

<!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 是不是没写啊

js不可修改css中的属性,只能为指定的class的元素添加内联样式(style)

原生JS:

var dom = document.getElementsByClassName('dtd')

for(var i=0,len=dom.length i<len i++){

    dom[i].style.color = 'red'

}

Jquery

$('.dtd').css({color:'red'})


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11949850.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存