jquery - 如何使用 CSS 编辑 jQuery 表格
问题描述
我有一个使用 jQuery 创建表的外部文件。我正在尝试将第一列设置为与其他列不同的宽度。我更愿意添加一些我可以在 css 中使用的东西,以使将来的编辑更容易。我正在努力使语法正确。我的一些尝试显示在代码注释中,但我尝试了其他尝试,但也失败了。
var $table = $('<table>');
$table.append()
// thead
.append('<thead>').children('thead')
.append('<tr />').children('tr').append('<th></th><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th>');
//tbody
var $tbody = $table.append('<tbody />').children('tbody');
// add row
$tbody.append('<tr />').children('tr:last')
//.append("<th class="dynamicTable-rowHead">name</td>") -console gives Uncaught SyntaxError: missing ) after argument list
//.append("<th>name</th>").setAttribute('width', '10%') //setAttribute is not a function
.append("<th>name</td>") //this works but cannot define a width in css without it affecting all table columns
.append("<td>val</td>")
.append("<td>val</td>")
.append("<td>val</td>")
.append("<td>val</td>")
.append("<td>val</td>")
.append("<td>val</td>")
.append("<td>val</td>");
// add table to dom
$table.appendTo('#dynamicTable');
解决方案
您只能使用 css 来完成。
var $table = $('<table>');
$table.append()
// thead
.append('<thead>').children('thead')
.append('<tr />').children('tr').append('<th></th><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th>');
//tbody
var $tbody = $table.append('<tbody />').children('tbody');
// add row
$tbody.append('<tr />').children('tr:last')
//.append("<th class="dynamicTable-rowHead">name</td>") -console gives Uncaught SyntaxError: missing ) after argument list
//.append("<th>name</th>").setAttribute('width', '10%') //setAttribute is not a function
.append("<th>name</td>") //this works but cannot define a width in css without it affecting all table columns
.append("<td>val</td>")
.append("<td>val</td>")
.append("<td>val</td>")
.append("<td>val</td>")
.append("<td>val</td>")
.append("<td>val</td>")
.append("<td>val</td>");
// add table to dom
$table.appendTo('#dynamicTable');
#dynamicTable th:first-child{
width:200px;
}
#dynamicTable th{
width:100px;
text-align:left;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="dynamicTable"></div>
推荐阅读
- python - 在 Cartopy 的正交投影上绘制风矢量
- python - 从烧瓶应用程序运行 celery apply_async 的问题
- python - django:发送到 ModelFormset 模板的 ajax 请求有问题
- javascript - 动画未动态分配给样式组件
- javascript - 数据在控制台中登录了几分之一秒,然后消失
- algorithm - 使用三叉树查找 minimun Vertext-Cover
- security - 破解 Rust 安全方法
- algorithm - 2D 迷宫中的 HMM 定位,难以应用平滑(后向算法)
- html - 垫图宽度填充
- docker - 为什么 http://localhost:9021/ 没有打开 Confluent 控制中心?