Change class using jquery

泄露秘密 提交于 2019-12-25 01:58:52

问题


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



来源:https://stackoverflow.com/questions/13880496/change-class-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!