Php 为每个表列添加列组

Php 为每个表列添加列组,php,jquery,html,css-tables,Php,Jquery,Html,Css Tables,可能还有其他类似的帖子,但这里什么都没有 我目前正在对一个现有站点进行返工,所需的一些更改包括列和行高亮显示,如此处(/) 由于有多个网页需要浏览,我希望有一些快捷方式来动态添加示例中的,而不必手动浏览每个页面和表 我考虑过php的preg\u replace函数,但我怀疑这是解决这个问题的最简单方法。在最佳情况下,我将能够验证每个列是否存在一个现有的数组。使用jQuery,您可以在突出显示脚本之前将动态地预先添加到每个表中。差不多- if($("table > colgroup").le

可能还有其他类似的帖子,但这里什么都没有

我目前正在对一个现有站点进行返工,所需的一些更改包括列和行高亮显示,如此处(/)

由于有多个网页需要浏览,我希望有一些快捷方式来动态添加示例中的
,而不必手动浏览每个页面和


我考虑过php的
preg\u replace
函数,但我怀疑这是解决这个问题的最简单方法。在最佳情况下,我将能够验证每个列是否存在一个现有的
数组。

使用jQuery,您可以在突出显示脚本之前将
动态地预先添加到每个表中。差不多-

if($("table > colgroup").length == 0){ // If the table does not have <colgroup></colgroup>

    var colCount = 0;
    $('tr:nth-child(1) td').each(function () { // Get the count of table columns
        if ($(this).attr('colspan')) { // if there is a <td colspan>
            colCount += +$(this).attr('colspan');
        } else {
            colCount++;
        }
    });


    var colgroupList = '';
    for (i=0;i<colCount;i++){ // Add a <colgroup></colgroup> for each <td>
        colgroupList += '<colgroup></colgroup>';
    }


    $("table").prepend(colgroupList);

}

$("table").delegate('td','mouseover mouseleave', function(e) {
 ...
因此,现在您的
$(“table”).delegate()
如下所示

$("table").delegate('td','mouseover mouseleave', function(e) {
   var $table = $(this).closest("table");
   if (e.type == 'mouseover') {
     $(this).parent().addClass("hover");
     $table.children("colgroup").eq($(this).index()).addClass("hover");
   } else {
     $(this).parent().removeClass("hover");
     $table.children("colgroup").eq($(this).index()).removeClass("hover");
    }
});
更新的JSFIDLE-

有3张桌子-

这非常整洁!但是,在具有多个表的页面上尝试此操作后,悬停对每一列都有效。请看,我已经使用一个选择器进行了更新,该选择器将仅获取当前表,并使用一个更新的JSFIDLE。
$("table").delegate('td','mouseover mouseleave', function(e) {
   var $table = $(this).closest("table");
   if (e.type == 'mouseover') {
     $(this).parent().addClass("hover");
     $table.children("colgroup").eq($(this).index()).addClass("hover");
   } else {
     $(this).parent().removeClass("hover");
     $table.children("colgroup").eq($(this).index()).removeClass("hover");
    }
});