Change class using jquery

2019-09-03 08:00发布

问题:

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

回答1:

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.



回答2:

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');


回答3:

$('tr td.rgExpandCol input.rgExpand').removeClass('oldClass').addClass('newClass');


回答4:

$(this).closest('td').find("input").removeClass("old").addClass("new")

Will apply the CSS for inputs inside the TD :)



回答5:

Try this:

$('.rgExpandCol > input').removeClass("< existing class >").addClass("< new class >");


回答6:

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');