我是靠谱客的博主 甜美毛巾,这篇文章主要介绍jquery怎样判断table中tr是否被选中,现在分享给大家,希望可以做个参考。

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

jquery怎样判断table中tr是否被选中

我们可以通过click()方法和index()方法来判断tr元素是否被选中。

click()方法表示当点击元素时,会发生 click 事件。当鼠标指针停留在元素上方,然后按下并松开鼠标左键时,就会发生一次 click,click() 方法触发 click 事件,或规定当发生 click 事件时运行的函数。

index() 方法返回指定元素相对于其他指定元素的 index 位置。

示例如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(function(){ var myRows = $('table tr').click(function(){ alert('Row #' +myRows.index(this)); }); }); </script> </head> <body> <table width="300" border="1" cellspacing="1" cellpadding="2"> <tr> <td width="150">0-1</td> <td width="150">0-2</td> </tr> <tr> <td>1-1</td> <td>1-2</td> </tr> <tr> <td>2-1</td> <td>2-2</td> </tr> <tr> <td>3-1</td> <td>3-2</td> </tr> <tr> <td>4-1</td> <td>4-2</td> </tr> </table> </body> </html>
登录后复制

输出结果:

04.png

选中第一行时,输出结果:

05.png

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

以上就是jquery怎样判断table中tr是否被选中的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是甜美毛巾最近收集整理的关于jquery怎样判断table中tr是否被选中的全部内容,更多相关jquery怎样判断table中tr是否被选中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(89)

评论列表共有 0 条评论

立即
投稿
返回
顶部