Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

jquery - jquery更改类名(jquery change class name)

I want to change the class of a td tag given the td tag's id:

(我想在给定td标签的id的情况下更改td标签的类:)

<td id="td_id" class="change_me"> ...

I want to be able to do this while inside the click event of some other dom object.

(我希望能够在其他dom对象的click事件中执行此操作。)

How do I grab the td's id and change its class?

(我如何获取td的id并更改其类?)

  ask by aeq translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can set the class (regardless of what it was ) by using .attr() , like this:

(您可以使用.attr() 设置类(无论它什么.attr() ,如下所示:)

$("#td_id").attr('class', 'newClass');

If you want to add a class, use .addclass() instead, like this:

(如果要添加类,请改用.addclass() ,如下所示:)

$("#td_id").addClass('newClass');

Or a short way to swap classes using .toggleClass() :

(或者使用.toggleClass()交换类的简短方法:)

$("#td_id").toggleClass('change_me newClass');

Here's the full list of jQuery methods specifically for the class attribute .

(以下是专门针对class属性的jQuery方法的完整列表 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...