Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 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 Reply

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方法的完整列表 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...