如何用jQuery选择除最后一列外的所有元素?

2026-03-31 14:051阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计418个文字,预计阅读时间需要2分钟。

如何用jQuery选择除最后一列外的所有元素?

在jQuery中,可以使用选择器$(':not(:last-child)’)排除最后一个元素;使用:last-child选择器可选择最后一个元素;而:not()选择器用于选择除指定元素外的其他元素。

在jquery中,可以利用“$("元素:not(:last-child)")”语句选择除了最后一列的其它元素;“:last-child”选择器可以选取最后一列元素,“:not()”选择器用于选取除指定元素以外的其它元素。

本教程操作环境:windows7系统、jquery3.2.1版本、Dell G3电脑。

jquery怎样选择除了最后一列其它的元素

如何用jQuery选择除最后一列外的所有元素?

我们可以通过:last-child选择器和:not(selector) 选择器来选择除了最后一列的元素。

示例如下:

<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $('tr td:not(:last-child)').css('background-color','red'); }); }); </script> </head> <body> <button>选择除了最后一列的所有元素</button> <table border="1"> <tr> <td>one</td><td>two</td><td>three</td><td>last</td> </tr> <tr> <td>blue</td><td>red</td><td>green</td><td>last</td> </tr> <tr> <td>Monday</td><td>Tuesday</td><td>Wednesday</td><td>last</td> </tr> </table> </body> </html>

输出结果:

点击按钮后:

相关视频教程推荐:jQuery视频教程

标签:其它元素

本文共计418个文字,预计阅读时间需要2分钟。

如何用jQuery选择除最后一列外的所有元素?

在jQuery中,可以使用选择器$(':not(:last-child)’)排除最后一个元素;使用:last-child选择器可选择最后一个元素;而:not()选择器用于选择除指定元素外的其他元素。

在jquery中,可以利用“$("元素:not(:last-child)")”语句选择除了最后一列的其它元素;“:last-child”选择器可以选取最后一列元素,“:not()”选择器用于选取除指定元素以外的其它元素。

本教程操作环境:windows7系统、jquery3.2.1版本、Dell G3电脑。

jquery怎样选择除了最后一列其它的元素

如何用jQuery选择除最后一列外的所有元素?

我们可以通过:last-child选择器和:not(selector) 选择器来选择除了最后一列的元素。

示例如下:

<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $('tr td:not(:last-child)').css('background-color','red'); }); }); </script> </head> <body> <button>选择除了最后一列的所有元素</button> <table border="1"> <tr> <td>one</td><td>two</td><td>three</td><td>last</td> </tr> <tr> <td>blue</td><td>red</td><td>green</td><td>last</td> </tr> <tr> <td>Monday</td><td>Tuesday</td><td>Wednesday</td><td>last</td> </tr> </table> </body> </html>

输出结果:

点击按钮后:

相关视频教程推荐:jQuery视频教程

标签:其它元素