在html表格中悬停时更改列颜色

时间:2017-07-12 14:02:44

标签: html css

当用户点击特定列的任何单元格时,我希望突出显示整列。目前,仅选择1个单元格的颜色变化。用途是突出显示该特定包装中提供的服务。请检查代码并告诉我如何解决它。

    <html>
    <head>
        <title>Digital Marketing Packages</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="description" content="Digital Marketing Packages" />
        <meta name="keywords" content="table, css3, style, beautiful, fancy, css"/>
        <link rel="stylesheet" href="table1style.css" type="text/css" media="screen"/>
    </head>
    <style>
        *{
            margin:0;
            padding:0;
        }
        body{
            font-family: Georgia, serif;
            font-size: 20px;
            font-style: italic;
            font-weight: normal;
            letter-spacing: normal;
            background: #f0f0f0;
        }
        #content{
            background-color:#fff;
            width:750px;
            padding:40px;
            margin:0 auto;
            border-left:30px solid #1D81B6;
            border-right:1px solid #ddd;
            -moz-box-shadow:0px 0px 16px #aaa;
        }
        .head{
            font-family:Helvetica,Arial,Verdana;
            text-transform:uppercase;
            font-weight:bold;
            font-size:12px;
            font-style:normal;
            letter-spacing:3px;
            color:#888;
            border-bottom:3px solid #f0f0f0;
            padding-bottom:10px;
            margin-bottom:10px;
        }
        .head a{
            color:#1D81B6;
            text-decoration:none;
            float:right;
            text-shadow:1px 1px 1px #888;
        }
        .head a:hover{
            color:#f0f0f0;
        }
        #content h1{
            font-family:"Trebuchet MS",sans-serif;
            color:#1D81B6;
            font-weight:normal;
            font-style:normal;
            font-size:56px;
            text-shadow:1px 1px 1px #aaa;
        }
        #content h2{
            font-family:"Trebuchet MS",sans-serif;
            font-size:34px;
            font-style:normal;
            background-color:#f0f0f0;
            margin:40px 0px 30px -40px;
            padding:0px 40px;
            clear:both;
            float:left;
            width:100%;
            color:#aaa;
            text-shadow:1px 1px 1px #fff;
        }

    </style>
    <body>
        <div id="content">
            <a class="back" href=""></a>
            <span class="scroll"></span>
            <p class="head">
                <a href="http://emsolutions.co.in/">Back to Excellentia Market Solutions</a>
            </p>
            <h1>Digital Marketing Packages</h1>          
            <table>
                <thead>
                    <tr>
                        <th></th>
                        <th scope="col" abbr="Starter" class="hovercss">Starter  $250</th>
                        <th scope="col" abbr="Deluxe" class="hovercss">Basic  $500</th>
                        <th scope="col" abbr="Medium" class="hovercss">Medium  $750</th>
                        <th scope="col" abbr="Business" class="hovercss">Business  $1000</th>
                        <th scope="col" abbr="Platinum" class="hovercss">Platinum  $1250</th>
                    </tr>
                </thead>
             <!--
                    <tr>
                        <th scope="row">Price Details</th>
                        <td>$250</td>
                        <td>$500</td>
                        <td>$750</td>
                        <td>$1000</td>
                        <td>$1250</td>
                    </tr>-->

                <tbody>
                <tr>
                  <th class="addcss">Initial Analysis</th>
                </tr>
                    <tr>
                        <th scope="row">Critical Website Analysis</th>
                        <td class="hovercss"><span class="check"></span></td>
                        <td class="hovercss"><span class="check"></span></td>
                        <td class="hovercss"><span class="check"></span></td>
                        <td class="hovercss"><span class="check"></span></td>
                          <td class="hovercss"><span class="check"></span></td>
                    </tr>
                    <tr>
                        <th scope="row">Back Link Analysis</th>
                         <td class="hovercss"><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>

                    </tr>
                    <tr>
                        <th scope="row">Competitor Analysis</th>
                        <td class="hovercss">1</td>
                         <td>2</td>
                        <td>3</td>
                        <td>4</td>
                        <td>5</td>
                    </tr>
                    <tr>
                        <th scope="row">Keyword/Phrase Analysis</th>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                    </tr>
                    <tr>
                        <th scope="row">Baseline Ranking check</th>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                    </tr>
                    <tr>
                        <th scope="row">Google Penalty Check</th>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                    </tr>
                    <tr>
                  <th class="addcss">SEO On-Page Optimization</th>
                </tr>
                    <tr>
                        <th scope="row">Keyword Research</th>
                        <td>10</td>
                        <td>25</td>
                        <td>40</td>
                        <td>55</td>
                          <td>70</td>
                    </tr>
                    <tr>
                        <th scope="row">Title Tag and Meta Description Optimization</th>
                         <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                    </tr>
                    <tr>
                        <th scope="row">Internal and External Link Optimization</th>
                           <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                    </tr>
                    <tr>
                        <th scope="row">Duplicate Content Check</th>
                          <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                    </tr>
                    <tr>
                        <th scope="row">Image Alt Tag Optimization</th>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                    </tr>
                    <tr>
                        <th scope="row">H1 Tag Optimization</th>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                    </tr>
                        <tr>
                        <th scope="row">Robots.txt</th>
                           <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                    </tr>
                    <tr>
                        <th scope="row">Canonicalization</th>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td> 
                        <td><span class="check"></span></td>
                    </tr>
                    <tr>
                        <th scope="row">Site Content Optimization</th>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                    </tr>
                    <tr>
                  <th class="addcss">SEO Off-Page Optimization</th>
                </tr>
                    <tr>
                        <th scope="row">Directory Submission</th>
                        <td>10</td>
                        <td>50</td>
                        <td>100</td>
                        <td>200</td>
                          <td>300</td>
                    </tr>
                    <tr>
                        <th scope="row">Social Bookmarking</th>
                        <td>20</td>
                        <td>40</td>
                        <td>80</td>
                        <td>120</td>  
                        <td>160</td>
                    </tr>
                    <tr>
                        <th scope="row">Articles / Press Release Writing and Submissions</th>
                        <td>4</td>
                        <td>8</td>
                        <td>12</td>
                          <td>16</td>
                          <td>20</td>
                    </tr>
                    <tr>
                        <th scope="row">Video Promotion</th>
                        <td>-</td>
                        <td>3</td>
                        <td>5</td>
                        <td>10</td>
                          <td>20</td>
                    </tr>
                    <tr>
                        <th scope="row">Blog Setup</th>
                        <td>1</td>
                        <td>2</td>
                        <td>4</td>
                        <td>6</td>
                          <td>8</td>
                    </tr>
                    <tr>
                        <th scope="row">Business Reviews</th>
                        <td>-</td>
                        <td>5</td>
                        <td>10</td>
                        <td>15</td>
                        <td>20</td>

                    </tr>
                     <tr>
                        <th scope="row">Classified Ads</th>
                        <td>-</td>
                        <td>10</td>
                        <td>20</td>
                        <td>30</td>
                        <td>50</td>

                    </tr>
                    <tr>
                  <th class="addcss">Social Promotions</th>
                </tr>
                    <tr>
                        <th scope="row">Facebook</th>
                      <td>-</td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                    </tr>
                    <tr>
                        <th scope="row">Twitter</th>
                      <td>-</td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                    </tr>
                    <tr>
                      <th scope="row">Google+</th>
                       <td>-</td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                    </tr>
                    <tr>
                        <th scope="row">Pinterest</th>
                     <td>-</td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                    </tr>
                    <tr>
                        <th scope="row">LinkedIn</th>
                        <td>-</td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                    </tr>
                      <th class="addcss">Online Reputation Management Reports</th>
                </tr>
                    <tr>
                        <th scope="row">Service for Negative Reviews Submission</th>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                    </tr>
                    <tr>
                        <th scope="row">Monthly Accomplishment Reports</th>
                       <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                        <td><span class="check"></span></td>
                          <td><span class="check"></span></td>
                    </tr>

                </tbody>
            </table>

        </div>

     </body>
    </html>

CSS Code :-

   /* Table 1 Style */
    table{
    font-family: "Times New Roman", sans-serif;
    font-size: 16px;
    font-weight: bold;
    line-height: 1.4em;
    font-style: normal;
    border-collapse:separate;
    }

    thead th{
    padding:15px;
    color:#fff;
     width: 550px;
    text-shadow:1px 1px 1px #568F23;
    border:1px solid #93CE37;
    border-bottom:3px solid #9ED929;
    background-color:#9DD929;
    background:-webkit-gradient(
        linear,
        left bottom,
        left top,
        color-stop(0.02, rgb(123,192,67)),
        color-stop(0.51, rgb(139,198,66)),
        color-stop(0.87, rgb(158,217,41))
        );
    background: -moz-linear-gradient(
        center bottom,
        rgb(123,192,67) 2%,
        rgb(139,198,66) 51%,
        rgb(158,217,41) 87%
        );
    -webkit-border-top-left-radius:5px;
    -webkit-border-top-right-radius:5px;
    -moz-border-radius:5px 5px 0px 0px;
    border-top-left-radius:5px;
    border-top-right-radius:5px;
    }
    thead th:empty{
    background:transparent;
    border:none;
    position:sticky;

    }
/*---------*/

   table {
   overflow: hidden;
    }

    tr, th, td:hover {
    background-color: white;
    }

    td, th {
    position: relative;
    }

    td:hover::after,
    th:hover::after {
    content: "";
    position: absolute;
    background-color: #ffa;
    left: 0;
    top: -5000px;
     height: 10000px;
     width: 100%;
     z-index: -1;
    }
     /*--nxt code--*/




    .addcss{

    color: #9DD929;
    padding: 10px;
    font-family: "Times New Roman;
    font-size: 24px;
     background:-webkit-gradient(
        linear,
        left bottom,
        right top,
        color-stop(0.02, rgb(255,255,255)),
        color-stop(0.51, rgb(139,198,66)),
        color-stop(0.87, rgb(123,192,67))

        );
     background: -moz-linear-gradient(
        left bottom,
        rgb(255,255,255) 2%,
        rgb(255,255,255) 51%,
        rgb(255,255,255) 87%
        );
    }
     tbody th{
    color:#fff;
    text-shadow:1px 1px 1px #568F23;
    background-color:#9DD929;
    border:1px solid #93CE37;
    border-right:3px solid #9ED929;
    padding:0px 10px;
    background:-webkit-gradient(
        linear,
        left bottom,
        right top,
        color-stop(0.02, rgb(158,217,41)),
        color-stop(0.51, rgb(139,198,66)),
        color-stop(0.87, rgb(123,192,67))

        );
    background: -moz-linear-gradient(
        left bottom,
        rgb(158,217,41) 2%,
        rgb(139,198,66) 51%,
        rgb(123,192,67) 87%
        );
    -moz-border-radius:5px 0px 0px 5px;
    -webkit-border-top-left-radius:5px;
    -webkit-border-bottom-left-radius:5px;
    border-top-left-radius:5px;
    border-bottom-left-radius:5px;
    width: 3000px;
    }

    tfoot td{
    color: #9CD009;
    font-size:32px;
    text-align:center;
    padding:10px 0px;
    text-shadow:1px 1px 1px #444;
    }
     tfoot th{
    color:#666;
    }
    tbody td{
    padding:10px;
    text-align:center;
    background-color:#DEF3CA;
    border: 2px solid #E7EFE0;
    -moz-border-radius:2px;
    -webkit-border-radius:2px;
    border-radius:2px;
    color:#666;
    text-shadow:1px 1px 1px #fff;
    }
     tbody span.check::before{
     content : url(../images/check0.png)
    }
    /*------------------------*/

2 个答案:

答案 0 :(得分:1)

我不确定这是否正是您正在寻找的内容,但您可以为每列中的所有内容提供相同的ID。例如:

<thead>
                <tr>
                    <th></th>
                    <th scope="col" abbr="Starter" id="1" class="hovercss">Starter  $250</th>
                    <th scope="col" abbr="Deluxe" id="2" class="hovercss">Basic  $500</th>
                    <th scope="col" abbr="Medium" id ="3" class="hovercss">Medium  $750</th>
                    <th scope="col" abbr="Business" id="4" class="hovercss">Business  $1000</th>
                    <th scope="col" abbr="Platinum" id="5" class="hovercss">Platinum  $1250</th>
                </tr>
            </thead>

确保&#34; Starter&#34;在表中也有id为1,依此类推。然后,您可以在onClick()方法中执行类似的操作,这需要一些JavaScript:

document.getElementById("1").className = "hightlightPackage";

OR

document.getElementById("1").classList.add("highlightPackage");

为完成目的,该类将类似于:

.highlightPackage {
    //what you want the column to look like when clicked
}

所以现在你要改变id = 1的所有东西的样式,这应该是整个列。一旦点击了不同的列,您可能还需要处理删除类。

答案 1 :(得分:1)

我不确定我是否理解你的需要,但我有一个建议,你可以在这里看到它:https://codepen.io/FaridNaderi/pen/rwooxr

注意:请点击列标题。

// main click event handler
   $('th').on('click',function(){
        var th = $(this);
         columnHighlight.css({
            left: th.offset().left - 6,  // 5 is padding-left of each cell + 1px border left
            width: th.width() + 10 // 10 is summ of padding-left and right of each cell
         });
   });

它实际上使用了桌子后面的div,然后在每次TH点击时,它根据TH大小和左偏移位置调整高亮度DIV的宽度和左CSS属性。

我还使用jQuery使其更易于阅读和理解,如果您需要原生JavaScript请告诉我

希望它有所帮助。