如何使用带包装器的Javascript/jQuery动态修改CSS类
How to dynamically modify CSS class using Javascript / jQuery with wrapper?
我有一个表的现有html代码。
例如。
<div class="content-entry">
<table class="old-style" border="3">
<tr align="middle">
<td style="font-weight:bold"></td>
</tr>
</table>
</div>
我想通过注入包装<div>
来有条件地删除当前表的所有属性/类,并使用Javascript/jQuery应用css类样式来覆盖现有的表行为。
conten-entry
可能有多个table
块,我想对所有这些块应用相同的样式更改。
至:
<div class="content-entry">
<div class="responsive-table">
<table class="table table-bordered table-condensed">
<tr>
<td></td>
</tr>
</table>
</div>
</div>
如何在不更改原始html代码的情况下做到这一点?
使用
.wrap
,将HTML结构包装在匹配元素集合中的每个元素周围
$('.old-style').wrap('<div class="responsive-table"></div>').removeClass().addClass('table table-bordered table-condensed');
.responsive-table {
color: red;
}
.table {
font-weight: bold;
}
.table-bordered {
border: 2px solid black;
}
.table-condensed>tbody>tr>td,
.table-condensed>tbody>tr>th,
.table-condensed>tfoot>tr>td,
.table-condensed>tfoot>tr>th,
.table-condensed>thead>tr>td,
.table-condensed>thead>tr>th {
padding: 5px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="content-entry">
<table class="old-style">
<tr>
<td>Hello</td>
</tr>
</table>
</div>
注意:调用不带参数的.removeClass
将删除该项的所有类。
相关文章:
- 动态jQuery不在.append()之后发布表单的添加输入
- 动态jQuery验证
- 动态 JQuery 视图在 django 中
- 动态jQuery变量名
- 基于html属性的动态jquery选择器
- 带有滑动图片的动态jQuery菜单
- 动态jQuery进度条-单击事件不工作
- 使用动态jQuery
- 如何获得动态 JQuery 移动手风琴.
- 基于当前内部HTML创建动态jQuery变量名称
- 动态 jquery ui 可排序块的索引/位置
- 动态 JQuery UI 中的表排序器不起作用
- 显示和隐藏动态 jQuery UI 对话框
- 使用 JQuery 在动态 JQuery UI 对话框中为动态创建的表单元素设置值
- 你怎么能让我选择=“;选择“;动态Jquery
- 动态jQuery Waypoints循环
- 动态JQuery下拉列表没有'不起作用
- 如何勾选“复选框”动态- jQuery移动版
- 动态jQuery工具提示
- 动态jQuery内容加载