如何使用 jQuery 按名称选择元素? [英] How can I select an element by name with jQuery?

查看:30
本文介绍了如何使用 jQuery 按名称选择元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格列,我想展开和隐藏.当我通过 class 而不是元素的 name 选择它时,jQuery 似乎隐藏了 元素.

I have a table column I’m trying to expand and hide. jQuery seems to hide the <td> elements when I select it by class but not by the element’s name.

例如:

$(".bold").hide(); // Selecting by class works.
$("tcol1").hide(); // Selecting by name does not work.

注意下面的 HTML.第二列的所有行都具有相同的 name.如何使用 name 属性创建此集合?

Note the HTML below. The second column has the same name for all rows. How could I create this collection using the name attribute?

<tr>
  <td>data1</td>
  <td name="tcol1" class="bold"> data2</td>
</tr>
<tr>
  <td>data1</td>
  <td name="tcol1" class="bold"> data2</td>
</tr>
<tr>
  <td>data1</td>
  <td name="tcol1" class="bold"> data2</td>
</tr>

推荐答案

您可以使用 jQuery 属性选择器:

$('td[name="tcol1"]')   // Matches exactly 'tcol1'
$('td[name^="tcol"]' )  // Matches those that begin with 'tcol'
$('td[name$="tcol"]' )  // Matches those that end with 'tcol'
$('td[name*="tcol"]' )  // Matches those that contain 'tcol'

这篇关于如何使用 jQuery 按名称选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆