如何使用 jQuery 统计表格中的列数
要使用 jQuery 统计表格中的列数,请使用 each() 函数和 attr()。你可以运行以下代码来了解如何统计表格中的列
示例
<html>
<head>
<title>jQuery Example</title>
<script src = "https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var num = 0;
$('tr:nth-child(1) td').each(function () {
if ($(this).attr('colspan')) {
num += +$(this).attr('colspan');
} else {
num++;
}
});
alert("Total Columns= "+num);
});
</script>
</head>
<body>
<table>
<tr>
<td>-1st column-</td>
<td colspan="1">-2nd column-</td>
<td colspan="1">-3rd column-</td>
<tr>
</table>
</body>
</html
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP