如何使用jquery将元素的标题添加到类属性

时间:2016-07-09 09:42:17

标签: javascript jquery html class

我已经看到可以将title属性转移到类。问题是如何找到第一个单词的元素(动画)并将它们分配给类标题?

例如:

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

从tittle中添加类

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

1 个答案:

答案 0 :(得分:1)

您可以使用.addClass( function )来完成这项工作。

&#13;
&#13;
$(".colelem").addClass(function(){
    return $(this).attr("title");
});
&#13;
.animated { 
    color: red;
}
&#13;
<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>
&#13;
&#13;
&#13;