我有一个表列,我正在尝试展开和隐藏。当我按类而不是按元素名称选择jQuery时,jQuery似乎隐藏了<td>元素。

例如:

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

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

<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>

当前回答

您可以使用以下功能:

get.elementbyId();

其他回答

您可以使用JQuery中的ID属性获取该元素,如下所示:

$("#tcol1").hide();

这里有一个简单的解决方案:$('td[name=tcol1]')

我已经这样做了,而且效果很好:

$('[name="tcol1"]')

https://api.jquery.com/attribute-equals-selector/

您可以使用以下功能:

get.elementbyId();

可以使用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'