Change class using jquery

2019-09-03 08:01发布

I have a td with follwoing class named 'rgExpandCol' and want to change the class of input inside it. below is my html:

<tr>
    <td class="rgExpandCol">
    <input class="rgExpand" ...
    </td>
<tr>

and I have tried this jquery but I get $find is null:

$(this).closest('td').attr('rgExpand', 'rgCollapse');

EDIT :::

basically am using radgrid and expanding rows which are hidden using

$(this).closest('tr').next('tr').css('display', '');

and $(this) is on my click event of grid as so

$("#<%=gv.ClientID%> tr:has(td)").click(function (e) { 

6条回答
Ridiculous、
2楼-- · 2019-09-03 08:28
$(this).closest('td').find("input").removeClass("old").addClass("new")

Will apply the CSS for inputs inside the TD :)

查看更多
迷人小祖宗
3楼-- · 2019-09-03 08:28

So you want to target the td and change the class of the input inside it?

I'd do this:

$('.rgExpandCol > input').removeClass('classtoremove').addClass('newclasstoadd');

查看更多
4楼-- · 2019-09-03 08:30

Use removeClass() and addClass() to make the change.

$("#<%=gv.ClientID%> tr:has(td)").click(function (e) {
    e.preventDefault();
    $(this).find('input').addClass('rgExpand').removeClass('rgCollapse');
});

Assuming that the id used in the selector identifies the table.

查看更多
聊天终结者
5楼-- · 2019-09-03 08:35

Try this:

$('.rgExpandCol > input').removeClass("< existing class >").addClass("< new class >");
查看更多
Root(大扎)
6楼-- · 2019-09-03 08:38
$('tr td.rgExpandCol input.rgExpand').removeClass('oldClass').addClass('newClass');
查看更多
Explosion°爆炸
7楼-- · 2019-09-03 08:39

First you need to select the input, which can ben done with a single selector :

var my_input = $(this).find('input.rgExpand');

Then you can remove its current class and add the new one :

my_input
    .removeClass('rgExpand')
    .addClass('rgCollapse');
查看更多
登录 后发表回答