How can I select an element by name with jQuery?

2018-12-31 10:42发布

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 element's name.

For example, why does:

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

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>

14条回答
只靠听说
2楼-- · 2018-12-31 11:02

You can use any attribute as selector with [attribute_name=value].

$('td[name=tcol1]').hide();
查看更多
深知你不懂我心
3楼-- · 2018-12-31 11:04

I've done like this and it works:

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

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

查看更多
登录 后发表回答