简体   繁体   中英

How to add title of element to class attribute using jquery

I have seen that is it possible to transfer title attribute to class. The question is how to find the elements of the first word (animated) and assign them to the class title?

For example:

<div class="colelem" title="animated infinite bounce"></div>
<div class="colelem" title="animated infinite bounce"></div>

Add the class from the tittle to happen

<div class="colelem animated infinite bounce" title="animated infinite bounce"></div>
<div class="colelem animated infinite bounce" title="animated infinite bounce"></div>

You can use .addClass( function ) to do this work.

 $(".colelem").addClass(function(){ return $(this).attr("title"); }); 
 .animated { color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="colelem" title="animated infinite bounce">A</div> <div class="colelem" title="animated infinite bounce">B</div> <div class="colelem" title="animated infinite bounce">C</div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM