我有 2 个表,第一个表有 2 列,第二个表有 3 列,我使用 ckeditor,所以我不知道如何添加不同的类名。所以在这个表中有类名“table”。我想计算每个表中有多少个第一列,如果第一列> 2,则给出包装类,到目前为止我的代码如下:
<script type="text/javascript">
$(document).ready(function(){
$('.prosiding-content iframe').wrap('<div class="map-responsive"></div>');
// $('.prosiding-content table').wrap('<div class="table-responsive"> </div>');
$('.prosiding-content table').addClass('table');
$('.prosiding-content img').addClass('img-responsive');
var table= $(".prosiding-content table").find("td:first").length;
if (table>2) {
$(this).wrap('<div class="table-responsive"> </div>');
}
});
</script>
最佳答案
您可以只对任何现有行内的列进行计数(假设表在行内具有相同的列数)并添加所需的类。
var tables = editor.editable().find( 'table' ).toArray();
CKEDITOR.tools.array.forEach( tables, function( table ) {
var header = table.findOne( 'tr' );
if ( header && header.getChildCount() > 2 ) {
table.addClass( 'multiple-columns' );
}
} );
请使用https://codepen.io/jacekbogdanski/pen/xzVYXL用于工作示例的 codepen。
关于javascript - 计算每个表中有多少个第一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50629617/