代码之家  ›  专栏  ›  技术社区  ›  MR.K

如何在没有元素子元素的元素中获取文本的css句柄

  •  1
  • MR.K  · 技术社区  · 10 年前

    如果我们使用 tbody tr th ,它给出了文本“显示模型示例标题”,包括其子元素(div)文本。 是否有可能只获取“Display model”而不获取其子元素“Sample Heading”文本的技巧?

    <table>
     <tbody>
       <tr>
         <th>
          Display model
          <div class="title">Sample Heading</div>
         </th>
        <td class="model">Data</td>
       </tr>
     </tbody>
    </table>
    
    1 回复  |  直到 10 年前
        1
  •  1
  •   Rino Raj    10 年前

    工作演示jQuery方式

    $(document).ready(function(){
        $('th').css('color','red').children().css('color','initial')
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
     <tbody>
       <tr>
         <th>
          Display model
          <div class="title">Sample Heading</div>
         </th>
        <td class="model">Data</td>
       </tr>
     </tbody>
    </table>

    工作演示CSS方式

    th{
      color:red;
    }
    th div{
      color:initial;
    }
    <脚本src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js“></script>
    <表格>
    <t车身>
    <tr>
    <第>
    显示模型
    <div class=“title”>示例标题</div>
    </第>
    <td class=“model”>数据</td>
    </tr>
    </t车身>
    </表格>